移动端 案例--轮播

涉及JS框架:zepto;相当于移动端的jq
			    移动端事件:
				touchstart:手按下去时
				touchmove:开始滑动
				touchend:滑动结束时
	 			js==jq;
				zepto===jq;onclick 移动端会有200-300ms的延时
					touch模块;---移动端的事件
					tap    点击事件
					swipeLeft: 向左滑动
					swipeRight:向右滑动
					swipeUp:    向上滑动
					swipeDown:   向下滑动
	


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
<!--//出现问题:轮播做好后,在最后一张切换到第一张时,出现延时.解决方法:-->

    <meta name="viewport"content="width=device-width,initial-scale=1.0">
    <style>
        *{margin: 0;padding: 0}
        li{list-style: none}
        a{text-decoration: none}
        body,html{width: 100%;height: 100%;overflow: hidden}
        .banner,.pic{width: 100%;height:200px;overflow: hidden;position: relative;}
        .banner .pic ul{width:1000%;}
        .banner .pic ul li{width: 10%;height: 200px;float: left;}
        .banner .pic ul li:nth-of-type(1){background: rgba(22,255,255,0.5);}
        .banner .pic ul li:nth-of-type(2){background: rgba(225,65,25,0.5);}
        .banner .pic ul li:nth-of-type(3){background: rgba(225,55,255,0.5);}
        .banner .pic ul li:nth-of-type(4){background: rgba(0,255,55,0.5);}
        .banner .pic ul li:nth-of-type(5){background: rgba(225,255,25,0.5);}
        .banner .pic ul li:nth-of-type(6){background: rgba(22,255,255,0.5);}
        .banner .pic ul li:nth-of-type(7){background: rgba(181, 255, 47, 0.5);}
        .banner .pic ul li:nth-of-type(8){background: rgba(22,255,255,0.5);}
        .banner .pic ul li:nth-of-type(9){background: rgba(22,255,255,0.5);}
        .banner .pic ul li:nth-of-type(10){background: rgba(255, 46, 67, 0.5);}


        .nav{position: absolute;right:50px;bottom: 20px; }
        .banner .nav li{float: left;background: rgba(252,25,255,0.1);width: 10px;height: 10px;border-radius: 50%;margin-right: 10px;}
        .banner .nav .active{background: rgba(252,25,255,1)}
    </style>
    <script src="js/zepto.js"></script>
    <script src="js/touch.js"></script>
    <script src="js/fx.js"></script>

    <script>


        $(function () {
            var moveUi=$(".ban")
            var index=0
            var vW=$('body').width();
            var aLi=$(".nav li")
            var timer=null

            //封装一个UL的移动;
            function moveUIRUN() {
                moveUi.animate({'transform':'translate('+index*vW*-1+'px)'});
                if(index>=9){
                    index=0;
                    moveUi.css({'transform':'translate('+index*vW*-1+'px)'});
                }else if(index<=0) {
                    index=9;
                    moveUi.css({'transform':'translate('+index*vW*-1+'px)'});
                }
                //设置nav标注高亮
                aLi.removeClass('active').eq(index).addClass('active');
                if(timer==undefined){
                    timer=setInterval(function () {
                        index++;
                        moveUIRUN()

                    },3000)
                }
            }
            //自动轮播
            timer=setInterval(function () {
                index++;
                moveUIRUN()
            },3000)

            //滑屏切换图片
            moveUi.swipeLeft(function () {
                //先清除定时器
                clearInterval(timer)
                timer=undefined;
                index++;

                moveUIRUN();

            })

            moveUi.swipeRight(function () {
                //先清除定时器
                clearInterval(timer)
                timer=undefined;
                index--;

                moveUIRUN();

            })



        })


    </script>

</head>
<body>
<div class="banner">
    <div class="pic">
        <ul class="ban">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
            <li>6</li>
            <li>7</li>
            <li>8</li>
            <li>9</li>
            <li>10</li>
        </ul>
    </div>

    <div class="nav">
        <ul>
            <li class="active"></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>


</div>

</body>
</html>

以下是一个使用 JavaScript 实现的简单的轮播案例: ```html <!DOCTYPE html> <html> <head> <title>JavaScript 案例轮播图</title> <style> #slideshow { position: relative; width: 600px; height: 400px; margin: 0 auto; overflow: hidden; } #slideshow img { position: absolute; top: 0; left: 0; width: 100%; height: 100%; opacity: 0; transition: opacity 1s ease-in-out; } #slideshow img.active { opacity: 1; } #slideshow button { position: absolute; top: 50%; transform: translateY(-50%); background-color: #fff; border: none; padding: 10px; cursor: pointer; font-size: 24px; } #slideshow button.prev { left: 0; } #slideshow button.next { right: 0; } </style> <script> var images = ["image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg"]; var index = 0; var slideshow = document.getElementById("slideshow"); var prevButton = document.createElement("button"); var nextButton = document.createElement("button"); function init() { prevButton.innerHTML = "❮"; prevButton.classList.add("prev"); prevButton.addEventListener("click", prevSlide); nextButton.innerHTML = "❯"; nextButton.classList.add("next"); nextButton.addEventListener("click", nextSlide); slideshow.appendChild(prevButton); slideshow.appendChild(nextButton); showSlide(); setInterval(nextSlide, 5000); } function showSlide() { var img = document.createElement("img"); img.src = images[index]; img.classList.add("active"); slideshow.appendChild(img); } function hideSlide() { var activeImg = slideshow.querySelector(".active"); activeImg.classList.remove("active"); slideshow.removeChild(activeImg); } function prevSlide() { hideSlide(); index--; if (index < 0) { index = images.length - 1; } showSlide(); } function nextSlide() { hideSlide(); index++; if (index >= images.length) { index = 0; } showSlide(); } window.addEventListener("load", init); </script> </head> <body> <h1>轮播图</h1> <div id="slideshow"></div> </body> </html> ``` 在这个案例中,我们使用了 JavaScript 来实现一个基本的轮播图。在 HTML 中,我们定义了一个包含图片的 div 元素,并使用 CSS 来设置其样式。在 JavaScript 中,我们定义了一个 images 数组来存储图片的文件名,一个 index 变量来记录当前显示的图片的索引,以及 prevButton 和 nextButton 两个按钮元素来控制切换图片。在 init 函数中,我们初始化轮播图,并设置了自动切换的定时器。在 showSlide 函数中,我们创建一个 img 元素,并将其添加到轮播图中。在 hideSlide 函数中,我们移除当前显示的图片。在 prevSlide 函数和 nextSlide 函数中,我们分别实现了向前和向后切换图片的逻辑。最后,我们在 window 的 load 事件中调用 init 函数来初始化轮播图。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值