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

Basic usage examples of listeners in Vue

Table of contents Preface 1. Basic usage of liste...

Summary of related functions for Mysql query JSON results

The JSON format field is a new attribute added in...

Complete example of Vue encapsulating the global toast component

Table of contents Preface 1. With vue-cli 1. Defi...

Vue realizes web online chat function

This article example shares the specific code of ...

Detailed process of SpringBoot integrating Docker

Table of contents 1. Demo Project 1.1 Interface P...

Introduction to generating Kubernetes certificates using OpenSSL

Kubernetes supports three types of authentication...

How to use HTML+CSS to create TG-vision homepage

This time we use HTML+CSS layout to make a prelim...

CocosCreator Getting Started Tutorial: Making Your First Game with TS

Table of contents premise TypeScript vs JavaScrip...

Introduction to the three essential logs for MySQL database interviews

Table of contents 1. redo log (transaction log of...

mysql calculation function details

Table of contents 2. Field concatenation 2. Give ...

Detailed explanation of flex layout in CSS

Flex layout is also called elastic layout. Any co...

Detailed explanation of mysql replication tool based on python

Table of contents 1. Introduction Second practice...

Learn MySQL in a simple way

Preface The database has always been my weak poin...

Vue realizes the card flip effect

This article example shares the specific code of ...