JavaScript implements long image scrolling effect

JavaScript implements long image scrolling effect

This article shares the specific code of JavaScript long picture scrolling for your reference. The specific content is as follows

The scrolling of long images will involve a timer:

Let's review the timer first:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Timer Review</title>
</head>
<body>
    <button id="start">Start</button>
    <button id="stop">Close</button>
    <script type="text/javascript">
        var start = document.getElementById("start");
        var stop = document.getElementById("stop");
        var num = 0,timer = null;

        start.onclick = function (){
            // When using a timer, clear the original timer first and then turn it on. You can try commenting out the clearInterval(timer); below and then click the turn on button multiple times to compare the effects. clearInterval(timer);
            timer = setInterval(function (){
                num++;
                console.log(num);
            },1000)
        }
        stop.onclick = function (){
            clearInterval(timer);
        }
    </script>
</body>
</html>

After reviewing the timer content, let's look at the code for long image scrolling:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Long picture scrolling effect</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        body{
   background-color: #000;
  }
        #box{
   width: 658px;
   height: 400px;
   border: 1px solid #ff6700;
   margin: 100px auto;
   overflow: hidden;
   position: relative;
  }
        #box img{
   position: absolute;
   top: 0;
   left: 0;
  }
        #box span{
            position: absolute;
            width: 100%;
            height: 50%;
            left: 0;
            cursor: pointer;
        }
        #box #top{
   top: 0;
  }
  #box #bottom{
   bottom: 0;
  }
    </style>
</head>
<body>
    <div id="box">
        <img src="img/timer.jpeg" alt="">
        <span id="top"></span>
        <span id="bottom"></span>
    </div>
    <script type="text/javascript">
        // 1. Get the event source var box = document.getElementById('box');
  var pic = document.getElementsByTagName('img')[0];
  var divTop = document.getElementById('top');
  var divBottom = document.getElementById('bottom');

        // 2. Add event var num = 0, timer = null;
        divBottom.onmouseover = function () {
            // Clear the previous acceleration effect clearInterval(timer);
   // Let the picture scroll down timer = setInterval(function () {
     num -= 10;
     // This -3666 is adjusted according to the picture if (num >= -3666) {
     pic.style.top = num + 'px';
    }else{
     clearInterval(timer);
    }
   },50);
  }
  divTop.onmouseover = function () {
   clearInterval(timer);
   // Let the picture scroll up timer = setInterval(function () {
     num += 10;
    if(num <= 0){
     pic.style.top = num + 'px';
    }else{
     clearInterval(timer);
    }
   },100);
  }
  // Stop scrolling when the mouse moves away box.onmouseout = function () {
   clearInterval(timer);
  }
    </script>
</body>
</html>

I won’t put the effect picture here, you can try it yourself if you need it (remember to find the long picture)

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:
  • JS realizes the effect of long picture scrolling up and down
  • JS picture seamless and smooth scrolling code
  • js realizes seamless scrolling of pictures
  • A Flash scrolling and rotating display image code generator implemented with js
  • JS implements automatic loop scrolling code for text or pictures inside div
  • js code to implement continuous scrolling of pictures in div+css layout
  • js jquery made continuous scrolling code for pictures
  • JavaScript code to achieve image loop scrolling effect
  • JS to achieve horizontal scrolling effect of pictures sample code
  • js realizes the effect of left and right scrolling of pictures

<<:  MySQL enables slow query (introduction to using EXPLAIN SQL statement)

>>:  Mysql experiment: using explain to analyze the trend of indexes

Recommend

How to obtain root permissions in a docker container

First, your container must be running You can vie...

Implementation code of Nginx anti-hotlink and optimization in Linux

Hide version number The version number is not hid...

Implementation of installing Docker in win10 environment

1. Enter the Docker official website First, go to...

17 excellent web designs carefully crafted by startups

Startups often bring us surprises with their unco...

Example tutorial on using the sum function in MySQL

Introduction Today I will share the use of the su...

Building command line applications with JavaScript

Table of contents 1. Install node 2. Install Comm...

Some tips for using less in Vue projects

Table of contents Preface 1. Style penetration 1....

Two ways to implement HTML page click download file

1. Use the <a> tag to complete <a href=&...

How to Develop a Progressive Web App (PWA)

Table of contents Overview Require URL of the app...

Introduction to the usage of exists and except in SQL Server

Table of contents 1. exists 1.1 Description 1.2 E...

Example code for implementing hexagonal borders with CSS3

The outermost boxF rotates 120 degrees, the secon...

3 functions of toString method in js

Table of contents 1. Three functions of toString ...

Implementation of multi-port mapping of nginx reverse proxy

Code Explanation 1.1 http:www.baidu.test.com defa...