[英]jQuery: div moving as it shouldn't .mouseover() .mouseout()
I am new to jQuery, I have this div with an image and text inside: on mouse-over the whole div should move down, and on mouse-leave it should return to its original position. 我是jQuery的新手,我的div中带有图像和文本:在鼠标悬停时,整个div应该向下移动,而在鼠标离开时,它应该返回其原始位置。 If I trigger the mouse-over on the text it works properly, but if I do the same on the parent div or on the image, the div continues to move up and down.
如果我在文本上触发鼠标悬停,则它可以正常工作,但是如果我在父div或图像上执行相同操作,则div会继续上下移动。
Here's my HTML: 这是我的HTML:
<div id="sidebar">
<a href="/">
<img src="//path" width="150" style="margin-bottom:5px;" />
</a>
<div id="mydesc"></div>
</div>
And my Javascript: 而我的Javascript:
$('#sidebar').mouseover(function(){
$('#mydesc').animate({
'marginTop':"30"
});
});
$('#sidebar').mouseout(function(){
$('#mydesc').animate({
'marginTop':"0"
});
});
This should fix things. 这应该可以解决问题。 I've had this problem before.
我以前有这个问题。 Just use .hover() with 2 functions http://api.jquery.com/hover/ .
只需将.hover()与2个函数http://api.jquery.com/hover/结合使用 。 First function is mouseenter and other function is mouseleave.
第一个功能是mouseenter,其他功能是mouseleave。 By using this way you less code and easier to keep up.
通过这种方式,您可以减少代码并更容易跟上。 http://jsfiddle.net/9urkfub2/
http://jsfiddle.net/9urkfub2/
$(document).ready(function(){
$("#sidebar").hover(
function(){
$('#mydesc').animate({
'marginTop':"30"
});
}, function(){
$('#mydesc').animate({
'marginTop':"0"
});
});
});
Use this CSS. 使用此CSS。
.grow {
display: inline-block;
-webkit-transition-duration: 0.3s;
transition-duration: 0.3s;
-webkit-transition-property: -webkit-transform;
transition-property: transform;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
-webkit-transform: translateZ(0);
-ms-transform: translateZ(0);
transform: translateZ(0);
box-shadow: 0 0 1px rgba(0, 0, 0, 0);
}
.grow:hover {
-webkit-transform: scale(1.1);
-ms-transform: scale(1.1);
transform: scale(1.1);
}
Example element after applying hover.css effect: 应用hover.css效果后的示例元素:
<a class="button grow">Add to Basket</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.