Please check this example.There is two item, and whenever I hover the first
item, the first image
disappears and the second image shows up. At the same time, there is a dialog
at the right side of the first
item show up. My question is that When I hover the first item, the position change.
I guess this is because I use opacity
. I use opacity:0 -> opacity: 1
to show the dialog
. But if I use display: none -> display: initial
, the bug disappears.
Thanks for your help.
const service = function () {
const email = $('.container .first')[0];
$(email).mouseover(function () {
$(this).addClass('hover')
})
$(email).mouseout(function () {
$(this).removeClass('hover')
})
}
service();
img {
margin: 0 auto;
}
.container {
position: fixed;
left: 300px;
background: #fff;
top: 50%;
transform: translateY(-50%);
color: #333;
width: 50px;
font-size: 10px;
border: .7px solid #dcddde;
border-radius: 3px;
}
.container .first {
position: relative;
width: 50px;
text-align: center;
margin: 6px 0;
margin-top: 8px;
}
.container div span {
width: 30px;
display: inline-block;
height: 36px;
line-height: 16px;
overflow: hidden;
}
.container .hover {
color:#0079ff;
}
.container .first .dialog {
border: .7px solid #dcddde;
/* display: none;*/ /* change this*/
opacity: 0;
position: absolute;
color: #333;
font-size: 15px;
top: -7px;
right: 60px;
width: 200px;
height: 43px;
border-radius: 2px;
line-height: 43px;
text-align: center;
background-color: #fff;
}
.container .first-hover-img {
display: none;
}
.container .hover .dialog {
display: initial;
transition: opacity .6s;
opacity: 1;
}
.container .hover .first-img {
display: none;
}
.container .hover .first-hover-img {
display: initial;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="first">
<img class="first-img" src="https://via.placeholder.com/30x30" alt="">
<img class="first-hover-img" src="https://via.placeholder.com/30x30" alt="">
<span>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Nihil, reiciendis.
</span>
<div class="dialog">
Lorem ipsum dolor sit amet.
</div>
</div>
<div class="second">
<img class="service-img" src="https://via.placeholder.com/30x30" alt="">
<span>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Nihil, reiciendis.
</span>
</div>
</div>