美文网首页轮播图
教你如何写一个淡入淡出且有持续时间条的轮播图,带轮播时间加载。

教你如何写一个淡入淡出且有持续时间条的轮播图,带轮播时间加载。

作者: lMadman | 来源:发表于2017-07-13 15:12 被阅读0次

话不多说,先呈上一副完整的代码:

//css代码
<style>
    html,body{
        padding: 0;
        margin: 0;
    }
    body .shuffling {
        position: relative;
        height: 336px;
        overflow: hidden;
    }

    body .shuffling .prev-btn, body .shuffling .next-btn {
        position: absolute;
        top: 50%;
        height: 66px;
        width: 34px;
        background-color: rgba(238,238,238,0.4);
        z-index: 10;
        margin-top: -33px;
        cursor: pointer;
        opacity: 0;
        -webkit-transition: all 0.3s;
        transition: all 0.3s;

    }
    body .next-btn img{
        opacity: 0.7;
    }
    body .prev-btn img{
        opacity: 0.7;
    }

    body .shuffling .prev-btn {
        left: 0;
    }

    body .shuffling .next-btn {
        right: 0;
    }

    body .shuffling .item-img {
        height: 336px;
        cursor: pointer;
        position: absolute;
        visibility: hidden;
        opacity: 0;
        -webkit-transition: all 0.5s;
        transition: all 0.5s;
        width: 100%;
        background-size: cover;
        background-position: center center;
    }
    body .shuffling:hover .prev-btn{
        opacity: 1;
    }
    body .shuffling:hover .next-btn{
        opacity: 1;
    }

    body .shuffling .item-img:nth-of-type(1) {
        background-image: url("img/banner-1.jpg");
    }

    body .shuffling .item-img:nth-of-type(2) {
        background-image: url("img/banner-2.jpg");
    }

    body .shuffling .item-img:nth-of-type(3) {
        background-image: url("img/banner-3.jpg");
    }

    body .shuffling .item-img:nth-of-type(4) {
        background-image: url("img/banner-4.jpg");
    }

    body .shuffling .img-active {
        visibility: visible;
        opacity: 1;
    }

    body .shuffling .round {
        position: absolute;
        bottom: 8px;
        left: 0;
        right: 0;
        text-align: center;
    }

    body .shuffling .round span {
        display: inline-block;
        height: 6px;
        width: 6px;
        border-radius: 50%;
        background-color: rgba(255, 255, 255, 0.6);
        cursor: pointer;
        margin: 2px;
    }

    body .shuffling .round .on-hover, body .shuffling .round .onhover {
        width: 60px;
        border-radius: 10px;
        position: relative;
    }

    body .shuffling .round .on-hover:after {
        content: '';
        position: absolute;
        top: 1px;
        left: 1px;
        width: 0;
        height: 4px;
        border-radius: 4px;
        background-color: white;
        -webkit-transition: all 5s;
        transition: all 5s;
        -webkit-animation: widthAll linear 5s infinite;
        animation: widthAll linear 5s infinite;
    }
    @-webkit-keyframes widthAll {
        0% {
            width: 0;
        }
        50% {
            width: 29px;
        }
        100% {
            width: 58px;
        }
    }

    @keyframes widthAll {
        0% {
            width: 0;
        }
        50% {
            width: 29px;
        }
        100% {
            width: 58px;
        }
    }
</style>
//html代码
<body>
<div class="shuffling hidden-xs hidden-sm">
    <div class="item-img img-active" data-index-value="1"></div>
    <div class="item-img" data-index-value="2"></div>
    <div class="item-img" data-index-value="3"></div>
    <div class="item-img" data-index-value="4"></div>
    <div class="round">
        <span class="on-hover" data-index-value="1"></span>
        <span data-index-value="2"></span>
        <span data-index-value="3"></span>
        <span data-index-value="4"></span>
    </div>
    <div class="prev-btn">
        ![](img/prev-btn.png)
    </div>
    <div class="next-btn">
        ![](img/next-btn.png)
    </div>
</div>
</body>
//js代码
<script>
    /*轮播图模块*/
    var time = "";
    var index = 1;
    $(function () {
        showimg(index);
        $(".round span,.block").hover(function () {
            clearTimeout(time);
            var icon=$(this).attr('data-index-value');
            $(".round span").removeClass("on-hover").eq(icon-1).addClass("onhover");
            $(".shuffling .item-img").removeClass('img-active').eq(icon-1).addClass('img-active');
        }, function () {
            var icon=$(this).attr('data-index-value');
            $(".round span").removeClass("onhover").eq(icon-1).addClass('on-hover');
            index=$(this).attr('data-index-value')> 3 ? 1 :parseInt($(this).attr('data-index-value'))+1;
            time = setTimeout("showimg(" + index + ")", 5000);
        });

        $('.prev-btn').on('click',function () {
            clearTimeout(time);
            var index = $('.on-hover').attr('data-index-value');
            if(index == '1'){
                each_img(4);
                each_round(4);
            }else{
                each_img(index-1);
                each_round(index-1);
            }
        });
        $('.next-btn').on('click',function () {
            clearTimeout(time);
            var index = $('.on-hover').attr('data-index-value');
            if(index == '4'){
                each_round(1);
                each_img(1);
            }else{
                each_round(parseInt(index)+1);
                each_img(parseInt(index)+1);
            }
        })
    });

    function showimg(num) {
        index = num;
        $(".round span").removeClass("on-hover").eq(index-1).addClass("on-hover");
        $(".shuffling .item-img").removeClass('img-active').eq(index-1).addClass('img-active');
        index = index + 1 > 4 ? 1 : index + 1;
        time = setTimeout("showimg(" + index + ")", 5000);
    }

    function each_round(num) {
        $('.round span').each(function () {
            var self = $(this);
            if($(self).attr('data-index-value') == num){
                $(self).addClass('on-hover');
            }else{
                $(self).removeClass('on-hover');
            }
        })
    }
    function each_img(num) {
        $('.item-img').each(function () {
            var self = $(this);
            if($(self).attr('data-index-value') == num){
                $(self).addClass('img-active').siblings().removeClass('img-active');
                index=$(self).attr('data-index-value')> 3 ? 1 :parseInt($(self).attr('data-index-value'))+1;
                time = setTimeout("showimg(" + index + ")", 5000);
            }
        });
    }
</script>

效果图大家可以先去http://ipaiban.com

这里需要注意的是:
1.想要淡入淡出的效果,一开始隐藏图必须使用visibility: hidden;这个属性,而不能使用display,不然达不到这种效果;
2.js中的计时器必须及时清零;
3.其实原理非常简单,主要是思想,大家看一下代码片段就明白了。
(复制替换图片可直接使用)

相关文章

  • 教你如何写一个淡入淡出且有持续时间条的轮播图,带轮播时间加载。

    话不多说,先呈上一副完整的代码: 效果图大家可以先去http://ipaiban.com 这里需要注意的是:1.想...

  • Slider - 轮播图

    简介: 用react开发的轮播图组件,支持淡入淡出、水平滚动、垂直滚动的无缝轮播效果。可自定义轮播内容。 API ...

  • jquery 轮播

    1.渐变轮播,图片淡入淡出 代码预览代码 2.无限循环滚动轮播效果 代码预览代码 3.全屏轮播(图片大加载比较慢)...

  • 5.首页轮播图

    轮播图框架(GitHub搜索)flutter_swiper : ^1.1.6 静态轮播图 根据网络请求数据动态加载轮播图

  • 个人博客—轮播器

    个人博客—轮播器 轮播器自动轮播,每张图片淡入淡出 控制按钮和图片描述跟随图片轮播 鼠标悬停图片上方则停止轮播,滑...

  • 轮播图(2)——基于JQ的左右滑动轮播

    上回书我们说到原生js淡入淡出效果的轮播图,这回我们说说左右滑动轮播图,由于需要缓动动画效果,原生js需要封装缓动...

  • 27.jquery 实战-轮播

    代码 1.实现如下轮播效果 要求:渐变轮播,图片淡入淡出轮播会自动循环像左向右点击会展示前/后图片底部显示轮播当前...

  • 以24小时为例,PHP根据开始时间和结束时间,检测时间段是否被占

    需求:轮播图管理,24小时内有多张轮播图,每张轮播图可以播放一个时间段,单位为小时hour。为避免冲突,在设置轮播...

  • swift常用第三方

    FSPagerView轮播图NVActivityIndicatorView加载指示器kingfisher图片加载第...

  • 组件

    题目1: 封装一个轮播组件轮播组件轮播组件2,使用IIFE 题目2: 封装一个曝光加载组件曝光加载组件 题目3: ...

网友评论

    本文标题:教你如何写一个淡入淡出且有持续时间条的轮播图,带轮播时间加载。

    本文链接:https://www.haomeiwen.com/subject/aemzqxtx.html