js轮播图动画

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

开发工具与关键技术:

作者:黄瑞民

撰写时间:2021/11/28

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

本次分享的案例是轮播图

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>轮播图</title>

    <link rel="stylesheet" href="css/luenbo.css">

 <style>

.container{

position: relative;

width: 500px;

height: 300px;

/* border: 1px solid red; */

overflow: hidden;

}

.list{

position: absolute;

width: 3000px;

height: 300px;

}

.list img{

float: left;

width: 500px;

height: 300px;

}

.container a{

display:none;

position: absolute;

top: 50%;

width: 30px;

line-height: 30px;

font-size: 30px;

color: #fff;

text-align: center;

background-color: #ddd;

}

.container a:first-of-type{

left: 1%;

}

.container a:last-of-type{

right: 1%;

}

.container:hover a{

display: block;

}

.container a:hover{

background-color: #666;

}

.container .buttons{

position: absolute;

bottom: 3%;

right: 3%;

}

.container .buttons span{

border: 2px solid rgb(1, 255, 255);

display: inline-block;

width: 20px;

line-height: 20px;

background-color: #ddd;

text-align: center;

font-size: 12px;

}

.container .buttons .on{

background-color: rgb(195, 0, 255);

color: #fff;

}

 </style>

</head>

<body>

    <div class="container" id="container">

        <!-- 轮播图片 -->

        <div class="list" id="list" style="left: -500px">

            <img src="#" alt="">

            <img src="#" alt="">

            <img src="#" alt="">

            <img src="#" alt="">

            <img src="#" alt="">

            <img src="#" alt="">

        </div>

        <!-- 左右箭头 -->

        <a class="prev" id="prev"><</a>

        <a class="next" id="next">></a>

        <div class="buttons" id="buttons">

            <span class="on" index="1">1</span>

            <span index="2">2</span>

            <span index="3">3</span>

            <span index="4">4</span>

        </div>

    </div>

    <script>

var kaiguan =false;/* 控制动画 */

var timer;/* 控制定时器 */

var index =1;/* 记录图片 */

window.onload = function(){/* 文档加载完成后立即执行 */

// 点击切换图片

var prev = document.getElementById("prev");

var next = document.getElementById("next");

var list = document.getElementById("list");

next.onclick = function(){

// console.log(123);

// console.log(list);

// console.log(list.style.left);

if(kaiguan==true){

return ;

}

if(index==4){

index=1;

}

else

{

index++;

}

animation(-500);

show();

}

prev.onclick = function(){

if(kaiguan==true){

return ;

}

if(index==1){

index=4;

}

else

{

index--;

}

animation(500);

show();

}

// 自动播放

function autoplay(){

timer = setInterval(function(){

next.onclick()

},3000);

}

autoplay();

//鼠标移入停止播放

var container = document.getElementById("container");

container.onmouseover = function(){/* 鼠标移入 */

clearInterval(timer);

}

//鼠标移出自动播放

container.onmouseout = autoplay;/* 鼠标移出 */

//点击跳转图片

var buttons  =document.getElementById("buttons").getElementsByTagName("span");

console.log(buttons);

for(var i=0;i<buttons.length;i++){

buttons[i].onclick = function(){

if (this.className=="on") {

return false;

}

var myindex = this.getAttribute("index");

console.log(myindex);

offset =(myindex-index)*-500;

animation(offset);

index = myindex;

show();

}

}

}

function animation(offset){

kaiguan=true;/* 表示动画正在执行 */

var times = 400;/* 动画总时间 */

var intervals = 10;/* 间隔时间 */

var speed = offset/(times/intervals);/* 位移速度 */

var newoffset = parseInt(list.style.left)+offset;/* 新的位移距离 */

function go(){

if((speed<0&&parseInt(list.style.left)>newoffset)||(speed>0&&parseInt(list.style.left)<newoffset)){

list.style.left = parseInt(list.style.left)+speed+"px";

setTimeout(function(){

go();

},intervals);

}

else

{

list.style.left=newoffset+"px";

if(parseInt(list.style.left)<-2000){

list.style.left = -500+"px";

}

if(parseInt(list.style.left)>-500){

list.style.left = -2000+"px";

}

kaiguan =false;

}

}

   go();

}

//标示当前图片

function show(){

var buttons  =document.getElementById("buttons").getElementsByTagName("span");

for(var i =0;i<buttons.length;i++){

if (buttons[i].className=="on") {

buttons[i].className="";

}

buttons[index-1].className ="on";

}

}

 </script>

</body>

</html>

代码上的img标签,图片自定

以上是本次案例的分享代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值