Home Web Front-end JS Tutorial Animation effects of native js carousel renderings (with code)

Animation effects of native js carousel renderings (with code)

Aug 22, 2018 pm 05:31 PM
Native js

The content of this article is about the animation effect of the native js carousel renderings (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Native JS-Carousel
Today I will write a carousel JS effect written in native JS.
Implementation principle:
1. Create an array and write the corresponding z-index, opacity, top, width for each picture;
2. The operation to realize rotation is to construct The first set of values ​​in the array is placed in the last set, and it will be executed once when the button is clicked.
Display renderings:
Animation effects of native js carousel renderings (with code)
html layout:

<p class="wrap" id="wrap">
    <p class="slide" id="slide">
        <ul>
            <li><a href=""><img src="/static/imghw/default1.png"  data-src="images/logo.png"  class="lazy"      style="max-width:90%"  style="max-width:90%" alt=""></a></li>
            <li><a href=""><img src="/static/imghw/default1.png"  data-src="images/slide.jpg"  class="lazy"      style="max-width:90%"  style="max-width:90%" alt=""></a></li>
            <li><a href=""><img src="/static/imghw/default1.png"  data-src="images/slide2.jpg"  class="lazy"      style="max-width:90%"  style="max-width:90%" alt=""></a></li>
            <li><a href=""><img src="/static/imghw/default1.png"  data-src="images/i1.jpg"  class="lazy"      style="max-width:90%"  style="max-width:90%" alt=""></a></li>
            <li><a href=""><img src="/static/imghw/default1.png"  data-src="images/sto.jpg"  class="lazy"      style="max-width:90%"  style="max-width:90%" alt=""></a></li>
        </ul>
        <p class="arrow" id="arrow">
            <a href="javascript:;" id="arrLeft" class="prev"></a>
            <a href="javascript:;" id="arrRight" class="next"></a>
        </p>
    </p></p>
Copy after login

css style:

* {            margin: 0;            padding: 0;        }

        ul {            list-style: none;        }

        .wrap {            width: 1200px;            margin: 100px auto;        }

        .slide {            height: 500px;            position: relative;            width: 1200px;        }

        .slide ul li {            position: absolute;            top: 0;            left: 0;            z-index: 1;        }

        .slide li img {            width: 100%;        }

        .arrow {            position: absolute;            width: 100%;            top: 50%;            opacity: 0;            z-index: 3;        }

        .prev,        .next {            position: absolute;            height: 110px;            width: 110px;            border-radius: 50%;            top: 50%;            //margin-top: -56px;            overflow: hidden;            text-decoration: none;        }
        .prev{            left: 0;            background: url("images/slider-icons.png") no-repeat left top;        }
        .next{            right: 0;            background: url("images/slider-icons.png") no-repeat right top;        }
Copy after login

JS part:
Next, we first store the style of the corresponding image in an array.

//写每张图片对应的样式
    var config = [
        {            "width": 400,            "top": 20,            "left": 50,            "opacity": 0.2,            "zIndex": 2
        },      //0
        {            "width": 600,            "top": 70,            "left": 0,            "opacity": 0.8,            "zIndex": 3
        },     //1
        {            "width": 800,            "top": 100,            "left": 200,            "opacity": 1,            "zIndex": 4
        },     //2
        {            "width": 600,            "top": 70,            "left": 600,            "opacity": 0.8,            "zIndex": 3
        },    //3
        {            "width": 400,            "top": 20,            "left": 750,            "opacity": 0.2,            "zIndex": 2
        }    //4
    ];
Copy after login

When the page is loaded, the pictures are scattered, that is, the array just created is called, and they are assigned to each picture one by one.

var list=my$("slide").getElementsByTagName("li"); //拿到所有li
        function assign() {    //分配函数
            for (var i=0;i<list.length;i++){
                animate(list[i],config[i],function () {
                    flag=true;
                });
            }
        }
        assign();
Copy after login

There will be left and right arrows when the mouse enters and leaves. Show and hide, the principle of clicking the button rotation is to change the first of the array to the last or the last group to the first. The flag is to control when the button is clicked to ensure that a set of animations is completed before the next rotation animation can continue.

//鼠标进入,左右焦点的p显示
        my$("wrap").onmouseover=function () {
            animate(my$("arrow"),{"opacity":1});
        };        //鼠标离开,左右焦点的p隐藏
        my$("wrap").onmouseout=function () {
            animate(my$("arrow"),{"opacity":0});
        };        //点击右边按钮事件
        my$("arrRight").onclick=function () {
            if (flag){
                flag=false;
                config.push(config.shift());     //把第一组值放到最后一组

                assign();
            }

        };        //点击左边按钮事件
        my$("arrLeft").onclick=function () {
            if (flag){
                flag=false;
                config.unshift(config.pop());   //把最后一组值放到第一组
                assign();
            }
        };
    };
Copy after login

Complete JS code:

<script>
    //变速动画函数
    function animate(element, json, fn) {
        clearInterval(element.timeId);   //清理定时器
        element.timeId = setInterval(function () {
            var flag = true;    //假设默认为当前值已经等于目标值
            for (var arrt in json) {                if (arrt == "opacity") {   //如果属性值为opacity
                    var current = getStyle(element, arrt) * 100;   //current和target先扩大100倍
                    target = json[arrt] * 100;                    var step = (target - current) / 10;
                    step = step > 0 ? Math.ceil(step) : Math.floor(step);
                    current += step;
                    element.style[arrt] = current / 100;   //运算完后缩小100倍
                } else if (arrt == "zIndex") {   //如果属性值为zindex
                    element.style[arrt] = json[arrt];
                } else {      //普通属性
                    var current = parseInt(getStyle(element, arrt));
                    target = json[arrt];                    var step = (target - current) / 10;
                    step = step > 0 ? Math.ceil(step) : Math.floor(step); //step大于零向上取整,小于零向下取整
                    current += step;
                    element.style[arrt] = current + "px";
                }                if (current != target) {
                    flag = false;
                }
            }            if (flag) {    //只有所有属性的当前值已经等于目标值,才清理定时器
                clearInterval(element.timeId);                if (fn) {     //回调函数
                    fn();
                }
            }
            console.log("当前位置:" + current + "目标位置:" + target + " 移动步数:" + step);   //测试函数
        }, 20);
    }    function getStyle(element, arrt) {
        return window.getComputedStyle ? window.getComputedStyle(element, null)[arrt] : element.currentStyle[arrt];

    }    function my$(id) {
        return document.getElementById(id);
    }    //写每张图片对应的样式
    var config = [
        {            &quot;width&quot;: 400,            &quot;top&quot;: 20,            &quot;left&quot;: 50,            &quot;opacity&quot;: 0.2,            &quot;zIndex&quot;: 2
        },      //0
        {            &quot;width&quot;: 600,            &quot;top&quot;: 70,            &quot;left&quot;: 0,            &quot;opacity&quot;: 0.8,            &quot;zIndex&quot;: 3
        },     //1
        {            &quot;width&quot;: 800,            &quot;top&quot;: 100,            &quot;left&quot;: 200,            &quot;opacity&quot;: 1,            &quot;zIndex&quot;: 4
        },     //2
        {            &quot;width&quot;: 600,            &quot;top&quot;: 70,            &quot;left&quot;: 600,            &quot;opacity&quot;: 0.8,            &quot;zIndex&quot;: 3
        },    //3
        {            &quot;width&quot;: 400,            &quot;top&quot;: 20,            &quot;left&quot;: 750,            &quot;opacity&quot;: 0.2,            &quot;zIndex&quot;: 2
        }    //4
    ];    var flag=true;     //假设动画全部执行完毕-----锁

    //页面加载的事件
    window.onload=function () {
        //1---散开图片
        var list=my$(&quot;slide&quot;).getElementsByTagName(&quot;li&quot;); //拿到所有li
        function assign() {    //分配函数
            for (var i=0;i&lt;list.length;i++){
                animate(list[i],config[i],function () {
                    flag=true;
                });
            }
        }
        assign();        //鼠标进入,左右焦点的p显示
        my$(&quot;wrap&quot;).onmouseover=function () {
            animate(my$(&quot;arrow&quot;),{&quot;opacity&quot;:1});
        };        //鼠标离开,左右焦点的p隐藏
        my$(&quot;wrap&quot;).onmouseout=function () {
            animate(my$(&quot;arrow&quot;),{&quot;opacity&quot;:0});
        };        //点击右边按钮事件
        my$(&quot;arrRight&quot;).onclick=function () {
            if (flag){
                flag=false;
                config.push(config.shift());     //把第一组值放到最后一组

                assign();
            }

        };        //点击左边按钮事件
        my$(&quot;arrLeft&quot;).onclick=function () {
            if (flag){
                flag=false;
                config.unshift(config.pop());   //把最后一组值放到第一组
                assign();
            }
        };
    };</script>
Copy after login

Related recommendations:

javascript image compression code

javascript realizes code sharing of province and city linkage

The above is the detailed content of Animation effects of native js carousel renderings (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do I create and publish my own JavaScript libraries? How do I create and publish my own JavaScript libraries? Mar 18, 2025 pm 03:12 PM

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

How do I optimize JavaScript code for performance in the browser? How do I optimize JavaScript code for performance in the browser? Mar 18, 2025 pm 03:14 PM

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

How do I debug JavaScript code effectively using browser developer tools? How do I debug JavaScript code effectively using browser developer tools? Mar 18, 2025 pm 03:16 PM

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How do I use source maps to debug minified JavaScript code? How do I use source maps to debug minified JavaScript code? Mar 18, 2025 pm 03:17 PM

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

TypeScript for Beginners, Part 2: Basic Data Types TypeScript for Beginners, Part 2: Basic Data Types Mar 19, 2025 am 09:10 AM

Once you have mastered the entry-level TypeScript tutorial, you should be able to write your own code in an IDE that supports TypeScript and compile it into JavaScript. This tutorial will dive into various data types in TypeScript. JavaScript has seven data types: Null, Undefined, Boolean, Number, String, Symbol (introduced by ES6) and Object. TypeScript defines more types on this basis, and this tutorial will cover all of them in detail. Null data type Like JavaScript, null in TypeScript

See all articles