MouseOver、hover() 或 css() 用于更改位置
我的页面上有一个图像,希望它在鼠标悬停或单击时更改位置,所以我尝试了几种方法,但找不到正确的开始。
所以我开始更改顶部位置,但这不起作用,我希望当鼠标悬停在图像上时,它会跳转到位置 x/y (可能是随机 x/y),如果我再次将鼠标悬停在图像上,它将跳转到另一个位置。
$('#image').one('click', function () {
$(this).css( 'top' : '+=200' );
});
但这不起作用,所以请有人能给我一些意见,我可以弄清楚该怎么做?
I have an image on my page and want that it change the position on mouseover or on click, so I tried several things but I cant find the right start.
So I started to change the top-position but that's not working, I would like that on mouseover the image it jumps to position x/y (maybe random x/y) if I mouseover it again it will jump to another position.
$('#image').one('click', function () {
$(this).css( 'top' : '+=200' );
});
But that's not working so please someone could give me some input that I can figure out what to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意,在为 top 设置值时,jQuery 不会自动获取之前设置的 top 值并为其添加 200(像普通编程语言一样)。如果您尝试在控制台中查看,您可能会将其视为执行错误。可以先获取top的值,然后加上200px,然后再设置。
这应该可行:
我必须将字符串解析为
css
返回的整数,您也可以通过某种仅返回值而不返回添加了“px”的值的方法来替换该额外操作,不知道有没有。Note that while setting values for top, jQuery will not automatically get the value of top set previously and add 200 to it(like a normal programming language). You could have seen this as an error in executing if you try to see in a console. You can get the value of top first and then add 200px to it and then set it again.
This should work:
I had to parse the string to an integer returned by
css
, you could also substitute that extra operation by some method that returns only the value and not the value with "px" added to it, dunno if there is one already.尝试这样的事情:
Try something like this: