js焦点轮播图

news/2024/7/24 1:43:27 标签: javascript

汇集网上焦点轮播图的实现方式,自己试了下,不过鼠标悬浮停止动画和鼠标离开动画播放好像没生效,不太明白,最后两行代码中,为什么可以直接写stop和play。不用加括号调用函数么?求懂的大神指点!

所用知识点:

1.DOM操作

2.定时器

3.事件运用

4.Js动画

5.函数递归

6.无限滚动大法(可以用js实现一个假图的制作。不过说实话一直理解不了假图的作用原理)

<style>
        * {
            margin: 0;
            padding: 0;

        }

        a {
            text-decoration: none;
        }

        body {
            padding: 20px;
        }

        #container {
            width: 600px; /*这里600x400是图片的宽高*/
            height: 400px;
            border: 3px solid #333;
            overflow: hidden; /*隐藏溢出的图片,因为图片左浮动,总宽度为4200*/
            position: relative;
        }

        #list {
            width: 4200px; /*这里设置7张图片总宽度*/
            height: 400px;
            position: absolute; /*基于父容器container进行定位*/
            z-index: 1;
        }

        #list img {
            float: left;
            width:610px;
        }

        #buttons {
            position: absolute;
            height: 10px;
            width: 100px;
            z-index: 2; /*按钮在图片的上面*/
            bottom: 20px;
            left: 250px;
        }

        #buttons span {
            cursor: pointer;
            float: left;
            border: 1px solid #fff;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background: #333;
            margin-right: 5px;
        }

        #buttons .on {
            background: orangered; /*选中的按钮样式*/
        }

        .arrow {
            cursor: pointer;
            display: none; /*左右切换按钮默认先隐藏*/
            line-height: 39px;
            text-align: center;
            font-size: 36px;
            font-weight: bold;
            width: 40px;
            height: 40px;
            position: absolute;
            z-index: 2;
            top: 180px;
            background-color: RGBA(0, 0, 0, .3);
            color: #fff;
        }

        .arrow:hover {
            background-color: RGBA(0, 0, 0, .7);
        }

        #container:hover .arrow {
            display: block; /*当鼠标放上去容器上面就显示左右切换按钮*/
        }

        #prev {
            left: 20px;
        }

        #next {
            right: 20px;
        }
    </style>

  

<div id="container">
    <div id="list" style="left: -600px;">
        <img src="5.jpg" alt="1"/>
        <img src="1.jpg" alt="1"/>
        <img src="2.jpg" alt="2"/>
        <img src="3.jpg" alt="3"/>
        <img src="4.jpg" alt="4"/>
        <img src="5.jpg" alt="5"/>
        <img src="1.jpg" alt="5"/>
    </div>
    <div id="buttons">
        <span index="1" class="on"></span>
        <span index="2"></span>
        <span index="3"></span>
        <span index="4"></span>
        <span index="5"></span>
    </div>
    <a href="javascript:void(0);" id="prev" class="arrow">&lt;</a>
    <a href="javascript:void(0);" id="next" class="arrow">&gt;</a></div>

 

<script>
        window.onload = function(){
            var container=document.getElementById('container');
            var list=document.getElementById('list');
            var buttons=document.getElementById('buttons').getElementsByTagName('span');
            var next=document.getElementById('next');
            var prev=document.getElementById('prev');
            var index=1;

            var len=5;//图片的数量
            var animated=false;//用于判断切换是否进行
            var interval=3000; //自动播放定时器的秒数,3秒
            var timer; //定时器
//            切换动画
            function animate(offset){
                animated=true; //切换进行中
                var time=300; //位移总时间
                var inteval=10; //位移间隔时间
                var speed=offset/(time/inteval); //每次位移量
                var newLeft=parseInt(list.style.left)+offset;
                var go=function(){
                    if((speed<0&&parseInt(list.style.left)>newLeft)||(speed>0&&parseInt(list.style.left)>newLeft)){
                        list.style.left=parseInt(list.style.left)+speed+'px';
                        setTimeout(go,inteval);
                    }else{
                        animated=false;
                        list.style.left=newLeft+'px';
                        if(newLeft<-3000){
                            list.style.left=-600+'px';
                        }
                        if(newLeft>-600){
                            list.style.left=-3000+'px';
                        }
                    }
                }
                go();
                /*var newLeft=parseInt(list.style.left)+offset;
                list.style.left=newLeft+'px';
                if(newLeft<-3000){
                    list.style.left=-600+'px';
                }
                if(newLeft>-600){
                    list.style.left=-3000+'px';
                }*/
            }

//            为按钮添加样式
            function showButton(){
                for(var i=0;i<buttons.length;i++){
                    if(buttons[i].className=='on'){
                        buttons[i].className='';
                        break;
                    }
                }
                buttons[index-1].className='on';
            }
            next.onclick=function(){
                if(animated){
                    return;
                }
                if(index==5){
                    index=1;
                }else{
                    index+=1;
                }
                animate(-600);
                showButton();
            }

            prev.onclick=function(){
                if(animated){
                    return;
                }
                if(index==1){
                    index=5;
                }else{
                    index-=1;
                }
                animate(600);
                showButton();
            }

//            通过循环为按钮添加点击事件
            for(var i=0;i<buttons.length;i++){
                buttons[i].onclick=function(){
                    if(animated){
                        return;
                    }
//                    当继续点击当前按钮的时候,不进行切换
                    if(this.className == 'on'){
                        return;
                    }
//                    通过获取按钮标签的自定义属性Index 得到索引值,再计算差值
                    var myIndex = parseInt(this.getAttribute('index'));
                    //真正的偏移量
                    var offset = -600*(myIndex-index);
                    animate(offset);
                    index=myIndex;
                    showButton();
                }
//                自动播放
                function play(){
                    timer=setTimeout(function(){
                        next.onclick();
                        play();
                    },interval)
                }
                function stop(){
                    clearTimeout(timer);
                }
                container.onmouseover=stop;
                container.onmouseout=play;

                play();
            }
        }
    </script>

 

<script>
window.onload = function(){
var container=document.getElementById('container');
var list=document.getElementById('list');
var buttons=document.getElementById('buttons').getElementsByTagName('span');
var next=document.getElementById('next');
var prev=document.getElementById('prev');
var index=1;

var len=5;//图片的数量
var animated=false;//用于判断切换是否进行
var interval=3000; //自动播放定时器的秒数,3秒
var timer; //定时器
// 切换动画
function animate(offset){
animated=true; //切换进行中
var time=300; //位移总时间
var inteval=10; //位移间隔时间
var speed=offset/(time/inteval); //每次位移量
var newLeft=parseInt(list.style.left)+offset;
var go=function(){
if((speed<0&&parseInt(list.style.left)>newLeft)||(speed>0&&parseInt(list.style.left)>newLeft)){
list.style.left=parseInt(list.style.left)+speed+'px';
setTimeout(go,inteval);
}else{
animated=false;
list.style.left=newLeft+'px';
if(newLeft<-3000){
list.style.left=-600+'px';
}
if(newLeft>-600){
list.style.left=-3000+'px';
}
}
}
go();
/*var newLeft=parseInt(list.style.left)+offset;
list.style.left=newLeft+'px';
if(newLeft<-3000){
list.style.left=-600+'px';
}
if(newLeft>-600){
list.style.left=-3000+'px';
}*/
}

// 为按钮添加样式
function showButton(){
for(var i=0;i<buttons.length;i++){
if(buttons[i].className=='on'){
buttons[i].className='';
break;
}
}
buttons[index-1].className='on';
}
next.onclick=function(){
if(animated){
return;
}
if(index==5){
index=1;
}else{
index+=1;
}
animate(-600);
showButton();
}

prev.onclick=function(){
if(animated){
return;
}
if(index==1){
index=5;
}else{
index-=1;
}
animate(600);
showButton();
}

// 通过循环为按钮添加点击事件
for(var i=0;i<buttons.length;i++){
buttons[i].onclick=function(){
if(animated){
return;
}
// 当继续点击当前按钮的时候,不进行切换
if(this.className == 'on'){
return;
}
// 通过获取按钮标签的自定义属性Index 得到索引值,再计算差值
var myIndex = parseInt(this.getAttribute('index'));
//真正的偏移量
var offset = -600*(myIndex-index);
animate(offset);
index=myIndex;
showButton();
}
// 自动播放
function play(){
timer=setTimeout(function(){
next.onclick();
play();
},interval)
}
function stop(){
clearTimeout(timer);
}
container.onmouseover=stop;
container.onmouseout=play;

play();
}
}
</script>

http://www.niftyadmin.cn/n/1206103.html

相关文章

RPA之家UiPath视频教程

【RPA之家】UiPath Studio下载安装和激活 https://www.bilibili.com/video/BV18J411H7K7 【RPA之家】UiPath变量的介绍和使用 https://www.bilibili.com/video/BV18J411H7gy 【RPA之家】UiPath第一个案例HelloWorld https://www.bilibili.com/video/BV18J411H7Vp 【RPA之家】Ui…

Vim常用的总结

地址定界遵循正则表达式。1、HJKL上下左右移动2、I插入3、ZZ保存退出4、w /PATH/TO/SOMEFIEL 保存在指定路径5、dd删除当前行6、yy复制当前行7、p粘贴编辑模式&#xff1a;地址定界&#xff1a;1&#xff0c;2表示1到2行/pattern/,/pattern/sg&表示地址定界找到的东西\(XX\…

翻译记忆软件-塔多思TRADO经典教程_2

Trados 中文简明教程Trados 中文简明教程1. 准备工作 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.1 准备MultiTerm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . …

【云栖精选】《云栖精选阿里巴巴技术实战2016年刊》重磅发布

阿里云云栖社区出品的《云栖精选阿里巴巴技术实战2016年刊》整合提炼了云栖社区2016一整年来积累的精华&#xff0c;全书141页。 《云栖精选阿里巴巴技术实战2016年刊》&#xff1a;收藏版、高清版、袖珍版 这里有2016阿里云的大事记&#xff0c;从数加平台正式上线到阿里云品牌…

【云扩RPA】SeperateCol

获取分隔列数据 此节课程将讲述如何使用获取结构化数据组件获取到分隔列数据 准备工作 打开云扩编辑器&#xff0c;新建一个空白项目&#xff0c;搜索序列组件并拖拽至设计面板&#xff0c;设为开始结点,双击打开序列打开Chrome浏览器&#xff0c;输入 “http://www.chinapo…

Spring boot中使用log4j记录日志

为什么80%的码农都做不了架构师&#xff1f;>>> 之前在Spring Boot日志管理 一文中主要介绍了Spring Boot中默认日志工具&#xff08;logback&#xff09;的基本配置内容。对于很多习惯使用log4j的开发者&#xff0c;Spring Boot依然可以很好的支持&#xff0c;只是…

【云扩RPA】SeperateCol_Quiz

获取到整表数据后可以选择不获取整表数据 是否 当检测到多页时&#xff0c;必须要指定下一个按钮吗&#xff1f; 是否 当流程中有绑定浏览器时&#xff0c;运行前必须提前打开指定页面 是否

处理一些兼容性的代码

1.兄弟节点解决兼容性方法&#xff1a; function getNextElementSibling(element) { var el element; while (el el.nextSibling) { if (el.nodeType 1) { return el; } } return null; } …