search
HomeWeb Front-endJS TutorialJavaScript implements carousel effect (code example)

本文给大家介绍JavaScript实现轮播图效果的,有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助。

学习前端也有一小段时间了,当初在学习javascript的时候,练手的一个轮播图实例,轮播图也是挺常见的了。

着是通过获取图片偏移量实现的。也实现了无缝切换。还有一点问题就是没有加上图片切换的时候的延迟了,哈哈 

html:

<div id="container">
        <div id="list" style="left: -600px;">
            <img src="/static/imghwm/default1.png"  data-src="../image/1.jpg"  class="lazy"   alt="5">
            <img src="/static/imghwm/default1.png"  data-src="../image/1.jpg"  class="lazy"   alt="1">
            <img src="/static/imghwm/default1.png"  data-src="../image/2.jpg"  class="lazy"   alt="2">
            <img src="/static/imghwm/default1.png"  data-src="../image/3.jpg"  class="lazy"   alt="3">
            <img src="/static/imghwm/default1.png"  data-src="../image/4.jpg"  class="lazy"   alt="4">
            <img src="/static/imghwm/default1.png"  data-src="../image/5.jpg"  class="lazy"   alt="5">
            <img src="/static/imghwm/default1.png"  data-src="../image/1.jpg"  class="lazy"   alt="1">
        </div>
        <div id="buttons">
                <span class="on"></span>
                <span></span>
                <span></span>
                <span></span>
                <span></span>
        </div>
        <a href="javascript:;" id="prev" class="arrow"><</a>
        <a href="javascript:;" id="next" class="arrow">></a>
    </div>

js:

window.onload = function(){
    //获取元素
    var container = document.getElementById(&#39;container&#39;);
    var list = this.document.getElementById(&#39;list&#39;);
    var buttons = document.getElementById(&#39;buttons&#39;).getElementsByTagName(&#39;span&#39;);
    var prev = document.getElementById(&#39;prev&#39;);
    var next = document.getElementById(&#39;next&#39;);
    var index = 1;//默认第一个小圆点亮

    //小圆点的点亮
    function showButton() {
        //遍历小圆点的个数,当触发onclick事件后,className为‘on’的变为‘’。
        for(var i = 0;i < buttons.length; i++){
            if(buttons[i].className == &#39;on&#39;){
                buttons[i].className = &#39;&#39;;
                break;
            }
        }
        buttons[index - 1].className = &#39;on&#39;; //原始第一个小圆点点亮,onclick事件触发后,index+1
    }

    function animate (offset) {
        //获取从第一张图片开始发生的偏移量
        var newLift = parseInt(list.style.left) + offset; 
        list.style.left = newLift + &#39;px&#39;;
        if(newLift > -600){  
            //如果偏移量的位置大于-600的时候,图片跳转到第五张图片
            list.style.left = -3000 + &#39;px&#39;;
        }
        if(newLift < -3000){ 
            //如果偏移量的位置大于-3000的时候,图片跳转到第一张图片
            list.style.left = -600 + &#39;px&#39;;
        }
    }
    next.onclick = function () {
        //如果button的index为5的时候,再点击next按钮会返回 1;
        if(index == 5){
            index = 1;
        }else{
            index += 1;
        }
        showButton();
        animate(-600);
    }
    prev.onclick = function () {
        if(index == 1){
            index = 5;
        }else{
            index -= 1;
        }
        showButton();
        animate(600);
    }
}

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。更多相关教程请访问JavaScript视频教程

相关推荐:

JavaScript图文教程

JavaScript在线手册

The above is the detailed content of JavaScript implements carousel effect (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software