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

Docker custom network container interconnection

Table of contents Preface –link Custom Network As...

Use CSS to implement special logos or graphics

1. Introduction Since pictures take up a lot of s...

Implementation of Vue single file component

I recently read about vue. I found a single-file ...

Example code for setting hot links and coordinate values ​​for web images

Sometimes you need to set several areas on a pict...

Create an SSL certificate that can be used in nginx and IIS

Table of contents Creating an SSL Certificate 1. ...

CentOS7 uses yum to install mysql 8.0.12

This article shares the detailed steps of install...

How to monitor mysql using zabbix

Zabbix deployment documentation After zabbix is ​...

Time zone issues with Django deployed in Docker container

Table of contents Time zone configuration in Djan...

Zen Coding Easy and fast HTML writing

Zen Coding It is a text editor plugin. In a text ...

Six inheritance methods in JS and their advantages and disadvantages

Table of contents Preface Prototype chain inherit...

How to configure Openbox for Linux desktop (recommended)

This article is part of a special series on the 2...

Tutorial on installing the unpacked version of mysql5.7 on CentOS 7

1. Unzip the mysql compressed package to the /usr...

A very detailed explanation of the Linux DHCP service

Table of contents 1. DHCP Service (Dynamic Host C...

Difference and implementation of JavaScript anti-shake and throttling

Table of contents 1. Anti-shake 2. Throttling 3. ...