我想要做的是当鼠标悬停在图像上时图像向右移动,当鼠标离开图像时返回到相同位置。但这的作用是图像向左移动并在鼠标悬停在其上方时消失。请告诉我我的错误是什么。
<html>
<head>
<style>
img { position: relative;
}
</style>
<script src="http://code.jquery.com/jquery-1.10.2.min.js" type = "text/javascript"></script>
<script type = "text/javascript">
$('#box img').hover(
function () {
$(this).animate({
right: '2px'
});
},
function () {
$(this).animate({
left: '2px'
});
}
);
</script>
</head>
<body>
<div id = "box">
<img src="pathtimage/1.jpg" hspace="30" height="350" width="220" >
</div>
</body>
</html>
最佳答案
jQuery 的 animate()
对 CSS 属性进行动画处理,因此您可能应该坚持对同一属性进行动画处理,因为 left
和 right
是两个不同的CSS 属性
$(function() {
$('#box img').hover(function () {
$(this).animate({
left: '2px'
}, 600);
}, function () {
$(this).animate({
left: '0px'
}, 600);
});
});
另请注意,在某些情况下,您必须在 CSS 中设置初始值,因为 jQuery 无法计算像 inherit
或 auto
这样的默认值,并且该设置保留如果没有位置,最高值就不起作用
#box img {position: relative; left: 0}
关于javascript - jQuery 悬停在上方无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21223947/