js之无缝轮播图

HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>轮播图</title>
<link rel="stylesheet" href="css/common.css"/>
<link rel="stylesheet" href="css/carousel.css"/>
</head>
<body>
    <div class="carousel">
        <ul class="carousel-list">
            <li><img src="img/1.jpg" /></li>
            <li><img src="img/2.jpg" /></li>
            <li><img src="img/3.jpg" /></li>
            <li><img src="img/4.jpg" /></li>
        </ul>
        <div class="direction-btn">
            <a href="javascript:;" class="prev">></a>
            <a href="javascript:;" class="next"><</a>
        </div>
        <div class="dots">
            <a href="javascript:;" class="active"></a>
            <a href="javascript:;"></a>
            <a href="javascript:;"></a>
            <a href="javascript:;"></a>
        </div>
    </div>
    <script src="js/common.js"></script>
    <script src="js/carousel.js"></script>
</body>
</html>

  

common.js

   /*
    缓冲运动
    obj:运动的对象
    target:运动属性和终点值的对象
    fn:回调函数
    ratio:运动系数,默认值为8
*/
function bufferMove(obj,target,fn,ratio){
    var ratio = ratio || 8;
    clearInterval(obj.timer);
    obj.timer = setInterval(function(){
        var allOK = true;
        for(var attr in target){
            var cur = 0;
            if(attr === ‘opacity‘){
                cur = parseInt(getStyle(obj,‘opacity‘) * 100);
            } else{
                cur = parseInt(getStyle(obj,attr));
            }
            var speed = (target[attr] - cur) / ratio;
            speed = speed > 0? Math.ceil(speed) : Math.floor(speed);
            var next = cur + speed;
            if(attr === ‘opacity‘){
                obj.style.opacity = next /100;
                obj.style.filter = ‘alpha( opacity = ‘ + next + ‘)‘;
            } else{
                obj.style[attr] = next + ‘px‘;
            }
            if(next !== target[attr]){
                allOK = false;
            }
        }
        if(allOK){
            clearInterval(obj.timer);
            if(fn){
                fn();
            }
        }
    },50);
}
//
///*
//    获取当前样式值
//*/
function getStyle(obj, attr) {
    if(obj.currentStyle) {
        return obj.currentStyle[attr];
    } else {
        return getComputedStyle(obj, false)[attr];
    }
}

//多属性函数封装

//封装获取样式的函数
//    function getStyle(obj,attr){
//        if(obj.currentStyle){
//            return obj.currentStyle[attr];
//        } else {
//            return getComputedStyle(obj,false)[attr];
//        }
//    }
//封装类名的获取
    function byClassName(sClassName){
        if(document.getElementsByClassName){
            return document.getElementsByClassName(sClassName);
        } else {
            var all = document.getElementsByTagName(‘*‘);
            var result = [];
            for(var i = 0;i <all.length;i++){
                if(all[i].className === sClassName){
                    result.push(all[i]);
                }
            }
            return result;
        }
    }
carousel.js
//    获取两个点击事件的元素
var oBtn = byClassName(‘direction-btn‘)[0],
//    获取大盒子
    oCarousel = byClassName(‘carousel‘)[0],
//    获取存储图片的ul
    oCarouselList = byClassName(‘carousel-list‘)[0],
//    获取ul下的li
    oCarouselLi = oCarouselList.children,
//    获取上一个的点击按钮
    oPrev = byClassName(‘prev‘)[0],
//    获取下一个的点击按钮
    oNext = byClassName(‘next‘)[0],
//  记录图片下标
    index = 0;
//    定义点的下标
    dotIndex = 0;
//    定义常量图片的宽度
    const iPerW = 590;
//    定义定时器
var    timer = null;
var oDots = byClassName(‘dots‘)[0];
var oDotsA = Array.from(oDots.children);
//遍历a
    oDotsA.forEach((v,k) =>{
        v.onmouseover = function(){
            index = k;
            dotIndex = k;
            bufferMove(oCarouselList,{left:-index * iPerW});
            setDotClass();
        }
    })

    function setDotClass(){
        //遍历每个a标签
        oDotsA.forEach(v => v.className=‘‘);
//        右侧a判断
        if(dotIndex >= oDotsA.length){
            dotIndex = 0;
        }
//        左侧a判断
        if(dotIndex < 0){
            dotIndex = oDotsA.length - 1;
        }
        oDotsA[dotIndex].className = ‘active‘;
    }

//添加第一张图片到末尾
oCarouselList.innerHTML += oCarouselLi[0].outerHTML;
//定义ul的总宽度
oCarouselList.style.width = iPerW * oCarouselLi.length + ‘px‘;
oCarousel.onmouseover = function(){
    clearInterval(timer);
    oBtn.style.display = ‘block‘;
}
oCarousel.onmouseout = function(){
    autoMove();
    oBtn.style.display = ‘none‘;
}
//添加点击事件
oNext.onclick = function(){
    rightMove();
}
function rightMove(){
    index++;
    dotIndex++;
    if(index >= oCarouselLi.length){
        index = 1;
        oCarouselList.style.left = ‘0px‘;
    }
    bufferMove(oCarouselList,{left:-index * iPerW});
    setDotClass();
}
//添加左侧点击事件
oPrev.onclick = function(){
    index--;
    dotIndex--;
    if(index < 0){
        index = oCarouselLi.length - 2;
        oCarouselList.style.left = - (oCarouselLi.length - 1)  * iPerW + ‘px‘;
    }
    bufferMove(oCarouselList,{left:-index * iPerW});
    setDotClass();
}
//自动播放
autoMove();
function autoMove(){
    timer = setInterval(function(){
        rightMove();
    },3000);
}

  

css文件

body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,blockquote,th,td{
    margin: 0;
    padding: 0;
}
body{
    font-family:"Mrcrosoft Yahei",Arial;
}
ul,ol{
    list-style: none;
}
a{
    text-decoration: none;
}
img{
    border:none;
}

body{
    background:lightblue;
}
.carousel{
    width: 590px;
    height: 340px;
    margin: 50px auto;
    overflow: hidden;
    position: relative;
}
.carousel-list{
    position: absolute;
}
.carousel-list li{
    float: left;
}
.direction-btn{
    display: none;
}
.direction-btn a{
    position: absolute;
    top: 50%;
    margin-top: -30px;
    width: 30px;
    line-height: 60px;
    background: rgba(0, 0, 0, .3);
    color: #fff;
    font-size: 30px;
    text-align: center;
}
.prev {
    left: 0;
}
.next {
    right: 0;
}
.dots{
    position: absolute;
    background:rgba(255,255,255,.4);
    border-radius: 12px;
    width: 72px;
    left: 0;
    right: 0;
    bottom: 10px;
    margin: 0 auto;
    font-size: 0;
    line-height: 0;
    padding: 0 4px;
}
.dots a{
    display: inline-block;
    width:10px;
    height: 10px;
    border-radius: 50%;
    background: #fff;
    margin: 6px 4px;
}
.dots .active{
    background: red;
}

  

图片更换成自己的路径即可

原文地址:https://www.cnblogs.com/bgwhite/p/9476330.html

时间: 2024-08-29 07:05:08

js之无缝轮播图的相关文章

JS 无缝轮播图1-节点操作

无缝轮播图 (使用节点操作) <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style> *{ padding:0; margin:0; } body{ background:#F5F5F5; } #content{ width:300px; height:200px;

左右无缝轮播图的实现

无缝轮播图: <title>无缝轮播图</title> <style> *{margin: 0;padding:0; } ul{list-style: none;} .banner{width: 600px;height: 300px;border: 2px solid #ccc;margin: 100px auto;position: relative;overflow: hidden;} .img{position: absolute;top: 0;left: 0}

原生JavaScript实现无缝轮播图

无缝轮播图是页面常用的特效之一,然而在实际的开发过程中,大部分的开发者都会使用插件来对轮播图进行开发,那么它的底层到底是怎么实现的呢,本文章将围绕这一问题展开探讨. 在讨论如何利用原生JS实现图片之间无缝切换轮播的动画效果之前,我们先来谈谈无缝轮播图片的css布局. 首先我们需要一个用来显示图片的DIV容器,然后把想要轮播的图片没有缝隙的排成一行放入DIV容器中,给DIV容器设置 overflow: hidden,这样在页面中就可以只看到一张图片,然后通过利用JS来移动ul的left值就能达到无

JS+css3焦点轮播图PC端

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>js原生web轮播图</title> 6 <style> 7 *{ 8 margin:0; 9 padding:0; 10 list-style: none; 11 -moz-user-select: none;/*文字不可选

JS学习笔记--轮播图效果

希望通过自己的学习收获哪怕收获一点点,进步一点点都是值得的,加油吧!!! 本章知识点:index this for if else 下边我分享下通过老师教的方式写的轮播图,基础知识实现: 1.css代码 <style> ul, ol { padding:0; margin:0; } li { list-style:none; } #box { width:600px; height:350px; border:10px solid #ccc; margin:0 auto; position:r

一个用原生JS造的轮播图插件

a native-js slider 一个无缝轮播图的小轮子 ( ?° ?? ?°)?? 前言 自己弄这个轮子是来自之前vue做一个音乐播放器项目时,用到了一个第三方vue组件better-scroll,当时参考这个文档做轮播图的时候,发现slider-item真实渲染出来的多了两个节点,向作者提问了下,回答当传入 snap:{loop:true} 的时候,前后各 clone 一个节点,保证可以无缝循环轮播,那么多克隆这两个节点是怎么实现无无缝轮播图呢,查阅了相关原理,就做了下这个轮子. 在线演

原生JS实现简易轮播图

原生JS实现简易轮播图(渐变?) 最近做网页总是会用到轮播图,我就把之前写的轮播图单独拿出来吧,如果有...如果真的有人也需要也可以复制去用用啊..哈~.. window.onload = function() { var tab = 0; var loop_imgs = new Array("img/l1.jpg", "img/l2.jpg", "img/l3.jpg", "img/l4.jpg", "img/l5

css3无缝轮播图案例

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <style> *{ margin: 0; padding: 0; } div{ width: 1000px; margin:300px auto; border:1px solid #ccc; overflow: hid

photoSlider-原生js移动开发轮播图、相册滑动插件

详细内容请点击 在线预览   立即下载 使用方法: 分别引用css文件和js文件 如: <link rel="stylesheet" type="text/css" href="css/photoSlider.min.css" /><script src="js/photoSlider.min.js" type="text/javascript" charset="utf-8&q