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 build your own Angular component library with DevUI

Table of contents Preface Creating a component li...

Summary of clipboard.js usage

Table of contents (1) Introduction: (2) The ways ...

Detailed explanation of table return and index coverage examples in MySQL

Table of contents Index Type Index structure Nonc...

Detailed explanation of script debugging mechanism in bash

Run the script in debug mode You can run the enti...

A brief analysis of the usage of USING and HAVING in MySQL

This article uses examples to illustrate the usag...

Detailed explanation of nginx configuration file interpretation

The nginx configuration file is mainly divided in...

My personal summary of mysql 5.7 database installation steps

1.mysql-5.7.19-winx64.zip (this is the free insta...

Two ways to manage volumes in Docker

In the previous article, I introduced the basic k...

Vue implements the full selection function

This article example shares the specific code of ...

Detailed discussion on the issue of mysqldump data export

1. An error (1064) is reported when using mysqldu...

Vue implements login type switching

This article example shares the specific code of ...

XHTML 1.0 Reference

Arrange by functionNN : Indicates which earlier ve...

A brief introduction to MySQL storage engine

1. MySql Architecture Before introducing the stor...

Detailed explanation of 7 SSH command usages in Linux that you don’t know

A system administrator may manage multiple server...