在其他网站看见类似效果,但代码有400多行且看不懂,我用60多行的代码给予实现。
实现原理:(1)利用绝对定位固定好起始位置;(2)利用遮罩将右轴右侧的部分遮住;(3)让右轴和遮罩同时同速度向右运动!
效果图:
代码如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>诏书</title> <style type="text/css"> * { margin: 0; padding: 0; } #animate { margin: 40px auto; width: 495px; height: 150px; position: relative; overflow: hidden; } #back { width: 495px; height: 150px; position: absolute; left: 0; top: 10px; background: url() no-repeat; } #left { position: absolute; left: 0; } #right { position: absolute; left: 16px; } #mark { position: absolute; left: 44px; } </style> </head> <body> <div> <div><img src="https://cdn.attach.qdfuns.com/notes/pics/201703/04/191214ug6h47d81jyfy6vh.png"/></div> <div><img src="https://cdn.attach.qdfuns.com/notes/pics/201703/04/191236gldigxmxg2zlh9s7.png"/></div> <div><img src="https://cdn.attach.qdfuns.com/notes/pics/201703/04/191244uhavf49l1zw440cv.png"/></div> <div><img src="https://cdn.attach.qdfuns.com/notes/pics/201703/04/191254kfbz2tjupc1jigbb.png"/></div> </div> </body> <script> var animate=document.getElementById("animate"); var right = document.getElementById("right"); var mark = document.getElementById("mark"); var timer = setInterval(function () { var right1=getComputedStyle(right).left; var mark1=getComputedStyle(mark).left; if(parseFloat(right1)>=447){ right1=447+"px"; clearInterval(timer); } right.style.left=(parseFloat(right1)+10)+"px"; mark.style.left=(parseFloat(mark1)+10)+"px"; }, 100) </script> </html>