【Jquery】Jquery 实现轮播图

1.jQuery是一个快速、简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(框架)于2006年1月由发布。

2.jQuery设计的宗旨是“write Less,Do More”,即倡导写更少的代码,做更多的事情。

3.它封装JavaScript常用的功能代码,提供一种简便的JavaScript设计模式,优化HTML文档操作、事件处理、动画设计和Ajax交互。

eg:

index.html

<!DOCTYPE html>
<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 href="common.css" type="text/css" rel="stylesheet" />
</head>

<body>

    <div class="wrap-box">
        <ul class="main-box">
            <li>
                <img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F14252964954%2F1000&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1669718027&t=a8534d34b1e45b63a026cca45238147a" alt="">
            </li>
            <li>
                <img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F14252973431%2F1000&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1669718027&t=e076026d8bdd93ce91bd7b364e0e553a" alt="">
            </li>
            <li>
                <img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F14123618464%2F641&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1669718185&t=6bc670d68d5f5e64c2594063427ac23d" alt="">
            </li>
            <li>
                <img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fci.xiaohongshu.com%2F32f16ed4-d791-3640-a17d-98b048722ddf%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fci.xiaohongshu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1669718184&t=eb0cfb1dee43fb887bf07ab5264e24e3" alt="">
            </li>
        </ul>

        <div id="pointsDiv">
            <span index="1" class="on"></span>
            <span index="2"></span>
            <span index="3"></span>
            <span index="4"></span>
        </div>

        <a href="javascript:;" id="prev" class="arrow">&lt;</a>
        <a href="javascript:;" id="next" class="arrow">&gt;</a>
    </div>

</body>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script>
    // dom 更新后获取的回调 
    $(function () {
        // 初始化样式 
        $(".main-box>li:first").siblings().css('display', 'none')
        $('span:first').css('background', 'red')

        // 标志位:设置显示图片的起始位置  
        let index = 0
        // 点击状态 
        let isToggleImagEnd = true;
        // 获取点击按钮事件回到 
        $('#next').click(function () {
            index === 3 ? index = 0 : index ++ 
            $(".main-box>li").eq(index).css("display", "block").siblings().css("display", "none")
            $('span').eq(index).css("background", 'red').siblings().css("background", "#ccc")
        });

        $('#prev').click(function () {
            index === 0 ? index = 4 :index -- 
            $(".main-box>li").eq(index).css("display", "block").siblings().css("display", "none")
        });

        // 圆点点击事件 
        $("span").click( function() {
            // this 指向调用者本省 
            index = $(this).index()
            $('.main-box>li').eq(index).css('display','block').siblings().css('display','none')
            $('span').eq(index).css('background',"red").siblings().css('background','pink')
        })
    })




</script>

</html>

common.css

* {
    margin: 0;
    padding: 0;
    text-decoration: none;
}

html {
    width: 100%;
    height: 100%;
}
/* 外包裹 */
.wrap-box {
    position: relative;
    width: 350px;
    height: 400px;
    margin: 200px auto;
}

/* 切换箭头 */
.arrow {
    cursor: pointer;
    display: none;
    display: inline-block;
    line-height: 39px;
    text-align: center;
    font-size: 36px;
    font-weight: bold;
    width: 40px;
    height: 40px;
    background-color: RGBA(0, 0, 0, 0.3);
    color: #fff;
}

/* 图片 */
img {
    width: 350px;
    height: 400px;
}

/* 图片包裹 */
.main-box>li {
    float: left;
}

/* 箭头位置 */
#prev {
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
}

#next {
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%); 
}

/* 小球样式 */
 /*包含所有圆点按钮的<div>*/
 #pointsDiv {
    position: absolute;
    height: 10px;
    width: 100px;
    z-index: 2;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
}

/*所有的圆点<span>*/
#pointsDiv span {
    cursor: pointer;
    float: left;
    border: 1px solid #fff;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: #333;
    margin-right: 8px;
}

/*鼠标移到切换图标上时*/
.arrow:hover {
    background-color: RGBA(0, 0, 0, 0.7);
}

实现效果 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用jQuery实现横向轮播图的示例代码: ```html <!DOCTYPE html> <html> <head> <title>横向轮播图</title> <style> .bannerCon { width: 100%; overflow: hidden; position: relative; } .bannerCon ul { width: 300%; position: absolute; left: 0; top: 0; list-style: none; margin: 0; padding: 0; } .bannerCon ul li { float: left; width: 33.33%; } .bannerCon ul li img { width: 100%; height: auto; } .bannerCon .prev, .bannerCon .next { position: absolute; top: 50%; transform: translateY(-50%); width: 30px; height: 30px; background-color: #ccc; color: #fff; text-align: center; line-height: 30px; cursor: pointer; } .bannerCon .prev { left: 10px; } .bannerCon .next { right: 10px; } .bannerCon .dots { position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%); list-style: none; margin: 0; padding: 0; } .bannerCon .dots li { display: inline-block; width: 10px; height: 10px; background-color: #ccc; border-radius: 50%; margin: 0 5px; cursor: pointer; } .bannerCon .dots li.active { background-color: #f00; } </style> </head> <body> <div class="bannerCon"> <ul> <li><img src="image1.jpg" alt="Image 1"></li> <li><img src="image2.jpg" alt="Image 2"></li> <li><img src="image3.jpg" alt="Image 3"></li> </ul> <div class="prev">Prev</div> <div class="next">Next</div> <ul class="dots"> <li class="active"></li> <li></li> <li></li> </ul> </div> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function() { var $bannerCon = $('.bannerCon'); var $ul = $bannerCon.find('ul'); var $li = $ul.find('li'); var $prev = $bannerCon.find('.prev'); var $next = $bannerCon.find('.next'); var $dots = $bannerCon.find('.dots li'); var currentIndex = 0; var interval; // 切换到指定索引的图片 function goToIndex(index) { $ul.animate({ left: -index * 100 + '%' }, 500); $dots.removeClass('active'); $dots.eq(index).addClass('active'); currentIndex = index; } // 自动播放 function startAutoPlay() { interval = setInterval(function() { var nextIndex = (currentIndex + 1) % $li.length; goToIndex(nextIndex); }, 2000); } // 停止自动播放 function stopAutoPlay() { clearInterval(interval); } // 点击上一张按钮 $prev.click(function() { var prevIndex = (currentIndex - 1 + $li.length) % $li.length; goToIndex(prevIndex); }); // 点击下一张按钮 $next.click(function() { var nextIndex = (currentIndex + 1) % $li.length; goToIndex(nextIndex); }); // 点击小圆点 $dots.click(function() { var index = $(this).index(); goToIndex(index); }); // 鼠标悬停时停止自动播放,移开时继续自动播放 $bannerCon.hover(stopAutoPlay, startAutoPlay); // 初始化 startAutoPlay(); }); </script> </body> </html> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值