Native JS to achieve image marquee effects

Native JS to achieve image marquee effects

Today I will share with you a picture marquee effect implemented with native JS. The effect is as follows:

The implemented code is as follows, you are welcome to copy and paste.

<!DOCTYPE html>
<html>
 
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Native JS to achieve image marquee effect</title>
 
    <style type="text/css">
        * {
            padding: 0;
            margin: 0;
        }
 
        li {
            list-style: none;
        }
 
        img {
            border: none;
        }
 
        body {
            background: #eee;
        }
 
        .slide-module {
            width: 120px;
            height: 400px;
            margin: 0 auto;
            background: #fff;
            border: 1px solid #ccc;
            position: relative;
            top: 50px;
        }
 
        .slide-module .up {
            width: 27px;
            height: 27px;
            /* Upward arrow */
            background: url(images/0.gif) no-repeat;
            position: absolute;
            top: 4px;
            left: 50%;
            margin-left: -16px;
            cursor: pointer;
            filter: alpha(opacity=60);
            opacity: 0.6;
        }
 
        .slide-module .down {
            width: 27px;
            height: 27px;
            /* Downward arrow */
            background: url(images/5.gif) no-repeat;
            position: absolute;
            bottom: 4px;
            left: 50%;
            margin-left: -16px;
            cursor: pointer;
            filter: alpha(opacity=60);
            opacity: 0.6;
        }
 
        .slide-module .wrap {
            width: 120px;
            height: 330px;
            position: absolute;
            top: 35px;
            left: 0;
            overflow: hidden;
        }
 
        .slide-module ul {
            width: 120px;
            position: absolute;
            top: 0;
            left: 0;
        }
 
        .slide-module li {
            width: 120px;
            height: 110px;
            float: left;
        }
 
        .slide-module a {
            display: block;
            width: 100px;
            height: 100px;
            border: 1px solid red;
            margin: 0 auto;
            position: relative;
            top: 4px;
        }
 
        .slide-module a:hover {
            border: 1px solid #333;
        }
 
        .slide-module .active {
            border: 10px solid #000;
        }
    </style>
    <script type="text/javascript">
 
        window.onload = function () {
            miaovSlide('miaovSlide');
        };
 
        function miaovSlide(o) {
            //Get the operation object container var obj = document.getElementById(o);
            if (!obj) return;
            //Get all the divs under the object
            var aDiv = obj.getElementsByTagName('div');
            //Get the up arrow var oUp = getClass('up');
            //Get the down arrow var oDown = getClass('down');
            //Get the image container var oWrap = getClass('wrap');
            //Get the image list var oUl = oWrap.getElementsByTagName('ul')[0];
            //Get the image list item var oLi = oUl.getElementsByTagName('li');
 
            var oTime = null;
            var iMs = 30;
            var i = 0;
            var iNum = 5;
            var toggle = -1;
 
            oUl.innerHTML += oUl.innerHTML;
 
            // When you click up, go up oUp.onclick = function () {
 
                toggle = -1;
 
                autoPlay(toggle);
            };
 
            // When clicking down, move forward oDown.onclick = function () {
                toggle = 1;
                autoPlay(toggle);
            };
 
            // When the up and down arrows are moved, change their transparency to 1
            oUp.onmouseover = oDown.onmouseover = function () {
                this.style.filter = 'alpha(opacity:100)';
                this.style.opacity = 1;
            };
 
            // When the up and down arrows move in, change their transparency to 0.6
            oUp.onmouseout = oDown.onmouseout = function () {
                this.style.filter = 'alpha(opacity:60)';
                this.style.opacity = 0.6;
            };
 
            // Image movement method, toggle represents the downward or upward value function autoPlay(toggle) {
                // Clear the original timer if (oTime) {
                    clearInterval(oTime);
                }
                // Restart the timer oTime = setInterval(function () {
                    // The second increment iNum += 2 * toggle;
                    // UL goes down, when the top value is greater than 0 if (iNum > 0) {
                        // Set the top value to half of the negative UL height (pull up)
                        iNum = -oLi.length * oLi[0].offsetHeight / 2;
                    }
                    // UL goes up, when the absolute value of the top value is greater than half of the UL's own width if (Math.abs(iNum) > oLi.length * oLi[0].offsetHeight / 2) {
                        // Set the top value to 0 (pull down)
                        iNum = 0;
                    }
                    // Assign to top value oUl.style.top = iNum + 'px';
 
                }, iMs);
            };
 
            autoPlay(toggle);
 
            // Get the element with the current style function getClass(sName) {
                for (i = 0; i < aDiv.length; i++) {
                    if (aDiv[i].className == sName) {
                        return aDiv[i];
                    }
                }
            }
        }
 
    </script>
</head>
 
<body>
 
    <div class="slide-module" id="miaovSlide">
        <!-- Up Arrow -->
        <div class="up"></div>
        <div class="wrap">
            <ul>
                <li>
                    <a>
                        <img src="images/1.jpg" />
                    </a>
                </li>
                <li>
                    <a>
                        <img src="images/2.jpg" />
                    </a>
                </li>
                <li>
                    <a>
                        <img src="images/3.jpg" />
                    </a>
                </li>
                <li>
                    <a>
                        <img src="images/4.jpg" />
                    </a>
                </li>
            </ul>
        </div>
        <!-- Down Arrow -->
        <div class="down"></div>
    </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:
  • Native JS to achieve the marquee effect
  • js to achieve text marquee effect
  • js marquee code (self-written)
  • javascript single line text scrolling up the marquee
  • JS code to implement the status bar marquee text effect
  • A simple example of implementing a marquee effect with Javascript
  • js text box moving marquee effect code sharing
  • JavaScript marquee hover zoom effect implementation code
  • Example of a marquee effect based on the Marquee.js plug-in
  • JavaScript Marquee Lottery Example

<<:  HTML table tag tutorial (3): width and height attributes WIDTH, HEIGHT

>>:  Implementation of fuzzy query like%% in MySQL

Recommend

Use js to write a simple snake game

This article shares the specific code of a simple...

Difference and principle analysis of Nginx forward and reverse proxy

1. The difference between forward proxy and rever...

How to configure Java environment variables in Linux system

Configure Java environment variables Here, the en...

js realizes two-way data binding (accessor monitoring)

This article example shares the specific code of ...

Solution to MySQL error code 1862 your password has expired

The blogger hasn't used MySQL for a month or ...

Summary of Common Commands for Getting Started with MySQL Database Basics

This article uses examples to describe the common...

React uses routing to redirect to the login interface

In the previous article, after configuring the we...

Detailed explanation of the usage of scoped slots in Vue.js slots

Table of contents No slots Vue2.x Slots With slot...

Example code of CSS layout at both ends (using parent's negative margin)

Recently, during the development process, I encou...

React passes parameters in several ways

Table of contents Passing parameters between pare...

Weather icon animation effect implemented by CSS3

Achieve results Implementation Code html <div ...

Vue realizes the card flip effect

This article example shares the specific code of ...

How to install the graphical interface in Linux

1. Linux installation (root user operation) 1. In...