时间:2020-12-03 16:48:04 | 栏目:JavaScript代码 | 点击:次
本效果使用js实现,当图片移动到200像素后回到0像素再向右移动200像素一直左右移动,不需要marquee标签。
完整的HTML代码如下,保存到HTML文档打开可以查看效果:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>JS使图片左右移动_何问起</title> <meta charset="utf-8" /> <base target="_blank" /> <style>a{color:blue;}</style> </head> <body> <p>mag:</p><input type="text" id="pos" /><br /> <input type="button" onclick="move()" value="开始" />请点击按钮<br /> <div style="position:relative;min-width:600px;min-height:120px;"> <img id="img" style="position:absolute; left:100px;" src="http://hovertree.com/themes/hvtimages/hlogo.png" /> </div> <div><a href="http://hovertree.com/h/bjaf/62ak7aig.htm">查看代码</a> <a href="http://hovertree.com">返回首页</a> <a href="http://hovertree.com/texiao/">更多特效</a></div> <script> var hovertreeStep, hovertreeTurn = true; function move() { var mag = parseInt(document.getElementById("img").style.left); document.getElementById("pos").value = mag; if (hovertreeTurn) { hovertreeStep = 1; hovertreeTurn = false; } if ( mag > 200) { hovertreeStep = -1; } if (mag < 1) { hovertreeStep = 1; } document.getElementById("img").style.left = (mag + hovertreeStep) + "px"; window.setTimeout("move()", 50); } </script> </body> </html>