Jq实现淡入淡出轮播图效果

如题所述,直接上代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    * {
        padding: 0px;
        margin: 0px;
    }

    /* 盒子  */
    .box {
        position: relative;
    }

    /* banner图 */
    .box,
    .banner {
        width: 1000px;
        height: 500px;
        margin: 50px auto;
    }

    .box .banner {
        position: relative;
        list-style: none;
    }

    .banner li {
        display: none;
    }

    .box ul li img {
        position: absolute;
    }

    .banner .b1 {
        display: block;
    }

    span {
        background: rgb(0, 0, 0, 0.5);
        width: 40px;
        height: 50px;
        display: block;
        position: absolute;
        left: 0px;
        top: 50%;
        z-index: 3;
        line-height: 50px;
        text-align: center;
        cursor: pointer;
        font-family: "宋体";
        font-size: 20px;
        color: white;
    }

    .right {
        left: auto;
        right: 0px;
    }

    .bottom {
        position: absolute;
        bottom: 10px;
        left: 50%;
        transform: translateX(-50%);
        list-style: none;
    }

    .bottom li {

        width: 10px;
        height: 10px;
        background: rgb(255, 255, 255);
        float: left;
        border-radius: 50%;
        margin: 0px 5px;
        /* 重影 */
        box-shadow: 0 0 5px rgba(0, 0, 0, 0.5);
        cursor: pointer;
    }

    .bottom .on {
        background: rgb(255, 153, 0);
    }
</style>
<script src="https://libs.baidu.com/jquery/2.0.0/jquery.js"></script>

<body>
    <div class="box">
        <!-- 轮播图 -->
        <ul class="banner">
            <li class="b1">
                <img src="../img/timg.jpg" alt="">
            </li>
            <li>
                <img src="../img/timg1.jpg" alt="">
            </li>
            <li>
                <img src="../img/timg2.jpg" alt="">
            </li>
            <li>
                <img src="../img/timg3.jpg" alt="">
            </li>
            <li>
                <img src="../img/timg4.jpg" alt="">
            </li>
        </ul>
        <!-- 左右切换 -->
        <span class="left">
            < </span> <span class="right">&gt;
        </span>
        <!-- 底部按钮 -->
        <ol class="bottom">
            <li class="on"></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ol>
    </div>
</body>
<script>
    var $box = $(".box");
    var $bli = $(".box ul li");
    var $bleft = $(".box .left");
    var $bright = $(".box .right");
    var $dot = $(".box .bottom li");
    var timer = null;
    var num = 0;
    // 因为几行代码一样 所以进行代码封装
    function play() {
        num = num % $bli.length;
        $bli.eq(num).stop().fadeIn(800).siblings().fadeOut(800);
        $dot.eq(num).addClass("on").siblings().removeClass("on");
    }

    function autoplay() {
        timer = setInterval(function () {
            num++;
            play();
        }, 2000);
    };
    autoplay();
    // 当鼠标放上去的时候 计时器停止   移除的时候计时器工作
    //  $box.hover(function() {
    //     clearInterval(timer);        
    //  },function() {
    //      autoplay();    
    //  });
    // 下面是同样的效果 另一种写法
    $box.mouseover(function () {
        clearInterval(timer);
    });
    $box.mouseout(function () {
        autoplay();
    });
    $bleft.click(function () {
        num--;
        play();
    });
    $bright.click(function () {
        num++;
        play();
    });
    $dot.click(function (event) {
        var index = $(this).index();
        num = index;
        $bli.eq(num).stop().fadeIn(800).siblings().fadeOut(800);
        $dot.eq(num).addClass("on").siblings().removeClass();
    });
</script>

</html>

其实淡入淡出轮播图的核心思想就是获取到当前的索引,根据索引找出图片的索引然后更改索引达到一个图片显示 其他图片消失 然后轮流交替进行的。最主要的核心思想在上面sibinling()中 通过这个方法 让当前元素进行淡入操作但是让其他元素进行淡出操作 所以效果上就形成了 ,一张张图盘轮流出来的效果。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
UniApp是一个基于Vue.js的跨平台框架,它可以帮助开发者构建一次编写、多端运行的应用。要实现淡入淡出的轮播样式,你可以按照以下步骤操作: 1. 首先,在项目中引入所需的UI组件库,如vant-weapp中的`van-swipe`组件,它包含了轮播功能。 ```html <template> <view class="swiper"> <van-swipe :autoplay="true" @change="onChange" style="height: 200px;"> <van-swipe-item v-for="(item, index) in items" :key="index"> <image :src="item.image" /> <!-- 使用v-if/v-else控制淡入淡出动画 --> <transition-group tag="div" name="fade"> <view v-if="index === currentIndex" class="active-slide"> {{ item.description }} </view> </transition-group> </van-swipe-item> </van-swipe> </view> </template> <script setup> import { ref } from 'vue'; import { swiper, swipeItem } from '@vant/weapp'; const items = ref([ // ... 你的轮播图数组,包括图片url和描述文本 ]); let currentIndex = ref(0); const onChange = (index) => { currentIndex.value = index; }; // 淡入淡出动画的CSS transition-group名称,可以根据需求自定义 <style scoped> .fade-enter-active, .fade-leave-active { transition: opacity .5s ease; } .fade-enter, .fade-leave-to { opacity: 0; } .active-slide { opacity: 1; } </style> </script> ``` 在这个例子中,我们使用了Vant Weapp的滑动组件,并通过`v-if/v-else`结合`transition-group`来实现轮播图淡入淡出效果。当切换到新的滑动项时,对应的slide会进入(`enter`),之前展示的slide则离开(`leave`),同时应用预设的CSS动画。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值