JavaScript timer to achieve seamless scrolling of pictures

JavaScript timer to achieve seamless scrolling of pictures

This article shares the specific code of JavaScript to achieve seamless scrolling of pictures for your reference. The specific content is as follows

text:

  • setInterval starts an interval timer
  • clearTimeout closes the timer
  • offsetWidth Get width
  • offsetLeft Get the left offset
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Seamless Mobility</title>
    <style>
        *{margin: 0; padding: 0;}
        #div1{width:520px; height:170px; margin:20px auto; position: relative; /* !!! The position of div1 is relative*/
                background: pink; overflow: hidden} /* !!! overflow: hidden */
        #div1 ul{position: absolute; left:0; top:0;} /* !!! ul's position: absolute, controls the value of left*/
        #div1 ul li{float:left; width:130px; height:170px; list-style: none}
    </style>
    <script>
        window.onload = function () {
            var oDiv = document.getElementById('div1');
            var oUl=oDiv.getElementsByTagName('ul')[0];
            var aLi=oUl.getElementsByTagName('li');
            var speed = 2;

            oUl.innerHTML+=oUl.innerHTML; // Equivalent to 4*2 images moving oUl.style.width=aLi[0].offsetWidth * aLi.length + 'px'; // !!!!!! offsetWidth

            function Move() {
                if(oUl.offsetLeft <- oUl.offsetWidth/2){ // Move halfway to the left and then come back oUl.style.left='0';
                }
                if(oUl.offsetLeft>0){ // // Move halfway to the right and then come back oUl.style.left = - oUl.offsetWidth/2 +'px';
                }
                oUl.style.left=oUl.offsetLeft + speed + 'px'; // !!!!!!!! offsetLeft
            }

            var Timer1 = setInterval(Move, 30); // setInterval turns on the interval timer oDiv.onmouseover = function () {
                clearTimeout(Timer1);
            };
            oDiv.onmouseout=function () {
                Timer1=setInterval(Move, 30);
            };

            document.getElementsByTagName('a')[0].onclick=function () {
                speed=-2; // speed to the left};
            document.getElementsByTagName('a')[1].onclick=function () {
                speed=2; // speed to the right};
        };
    </script>

</head>
<body>
<a href="javascript:;" >Move left</a>
<a href="javascript:;" >Move right</a>
    <div id="div1">
        <ul>
            <li><img src="img/aa.jpg"/></li>
            <li><img src="img/bb.jpg"/></li>
            <li><img src="img/cc.jpg"/></li>
            <li><img src="img/dd.jpg"/></li>
        </ul>
    </div>
</body>
</html>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of the JavaScript timer principle
  • Detailed explanation of JavaScript timers
  • JavaScript Timer Details
  • JavaScript timer to achieve limited time flash sale function
  • Summary of JavaScript Timer Types

<<:  MySQL scheduled task example tutorial

>>:  Detailed explanation of the solution to the problem of nohup log output being too large under Linux

Recommend

Summary of three methods of lazy loading lazyLoad using native JS

Table of contents Preface Method 1: High contrast...

Summary of 16 XHTML1.0 and HTML Compatibility Guidelines

1. Avoid declaring the page as XML type . The pag...

Implementation of CSS dynamic height transition animation effect

This question originated from a message on Nugget...

MySQL detailed explanation of isolation level operation process (cmd)

Read uncommitted example operation process - Read...

MySQL select results to perform update example tutorial

1. Single table query -> update UPDATE table_n...

In-depth explanation of Set and WeakSet collections in ES6

Table of contents Set is a special collection who...

Detailed explanation of the problem of CSS class names

The following CSS class names starting with a num...

ul list tag design web page multi-column layout

I suddenly thought of this method when I was writi...

How to package the project into docker through idea

Many friends have always wanted to know how to ru...

Advertising skills in the Baidu Union environment (graphic tutorial)

Recently, students from the User Experience Team o...

How to quickly install RabbitMQ in Docker

1. Get the image #Specify the version that includ...

How to test the maximum number of TCP connections in Linux

Preface There is a misunderstanding about the max...

Solution for Baidu site search not supporting https (tested)

Recently, https has been enabled on the mobile ph...

Detailed discussion of several methods for deduplicating JavaScript arrays

Table of contents 1. Set Deduplication 2. Double ...

How to add default time to a field in MySQL

Date type differences and uses MySQL has five dat...