Image 鼠标悬停或悬停()或css()用于更改位置
我在我的页面上有一个图像,希望它能改变鼠标或点击的位置,所以我尝试了几种方法,但我找不到正确的开始 所以我开始改变顶部的位置,但这不起作用,我希望鼠标在图像上方跳到位置x/y(可能是随机的x/y),如果我再次鼠标在上面,它将跳到另一个位置Image 鼠标悬停或悬停()或css()用于更改位置,image,hover,position,mouseover,Image,Hover,Position,Mouseover,我在我的页面上有一个图像,希望它能改变鼠标或点击的位置,所以我尝试了几种方法,但我找不到正确的开始 所以我开始改变顶部的位置,但这不起作用,我希望鼠标在图像上方跳到位置x/y(可能是随机的x/y),如果我再次鼠标在上面,它将跳到另一个位置 $('#image').one('click', function () { $(this).css( 'top' : '+=200' ); }); 但这不起作用,所以请有人给我一些信息,让我知道该怎么办?试试这样的方法: <html
$('#image').one('click', function () {
$(this).css( 'top' : '+=200' );
});
但这不起作用,所以请有人给我一些信息,让我知道该怎么办?试试这样的方法:
<html>
<head>
<script type="text/javascript">
function switchPos(obj)
{
var divobj = document.getElementById('div1')
var x = divobj.offsetHeight - obj.height - 5;
var y = divobj.offsetWidth - obj.width - 5;
var randomx = Math.floor(Math.random()*x+1);
var randomy = Math.floor(Math.random()*y+1);
obj.style.top = randomx + 'px';
obj.style.left = randomy + 'px';
}
</script>
</head>
<body>
<div id="div1" style="width: 800px; background: red;">
<img src="image.jpg" style="position:relative;" onmouseover="switchPos(this);" onmouseout="switchPos(this);"/>
</div>
</body>
</html>
功能开关位置(obj)
{
var divobj=document.getElementById('div1')
var x=偏离视线的目标-目标高度-5;
变量y=divobj.offsetWidth-obj.width-5;
var randomx=Math.floor(Math.random()*x+1);
var randomy=Math.floor(Math.random()*y+1);
obj.style.top=randomx+‘px’;
obj.style.left=randomy+'px';
}
请注意,在为top设置值时,jQuery不会自动获取top set的值并向其添加200(就像普通编程语言一样)。如果您尝试在控制台中查看,则可能会将此视为执行时的错误。您可以先获取top的值,然后将200px添加到它,然后再次设置它
这应该起作用:
$('#image').one('click', function () {
var top = parseInt($(this).css("top"));
$(this).css( 'top' , top+ 200);
});
我必须将字符串解析为css返回的整数,你也可以用一个只返回值而不返回值的方法代替这个额外的操作,并在其中添加“px”,不知道是否已经有一个了。啊,非常感谢你的开始。非常感谢:)如果我有一个div(宽度为800px),我如何防止图像跳出?谢谢您将保留此片段供以后使用css()进行更多测试;