基于jQuery实现自动或点击切换效果

该博客介绍如何利用jQuery实现元素的自动切换和点击切换效果。内容包括通过设置全局变量和自增变化来控制自动切换,以及在点击下方小圆点时,隐藏当前显示元素并显示对应的其他元素。同时,文章详细讲解了如何为小圆点添加单击事件以实现点击切换功能。

效果图

在这里插入图片描述

功能:元素自动切换,当点击下方小圆点时,控制上方元素同步切换

方法:主要是定义一个全局变量,通过自增变化来控制自动切换效果,其中当自身显示时,兄弟元素隐藏;给小圆点添加单击事件,当点击时,传递自身对象给单击切换函数,通过对象获取点击对象的下标值,从而切换元素

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>标题</title>
    <link rel="stylesheet" href="../css/reset.css">
    <script src="./jquery-3.4.1.min.js"></script>
    <style type="text/css">
        #box{
            width: 800px; height: 340px; 
            margin: 100px auto;
            position: relative;
        }
        .aaa{
            width: 800px; height: 300px; 
            line-height: 300px; text-align: center;
            font-size: 100px; color: #fff;
            position: absolute; top: 0; left: 0;
        }
        #div1{
            background: red;
            z-index: 1;
        }
        #div2{
            background: green;
        }
        #nav{
            width: 100px; height: 20px; 
            position: absolute; bottom: 0; left: 50%;
            margin-left: -50px;
        }
        #nav p{
            width: 20px; height: 20px;
            border-radius: 10px;
            background: #ccc;
            margin: 0 10px;
            float: left;
            cursor: pointer;
        }
        #nav .active{
            width: 40px; 
            background: blue;
        }
    </style>
    <script>
        $(function(){
            // 全局变量
            var c = 0;
            var aaa = $("#box .aaa");
            // 定义自动切换效果
            function gunDongBoFang(){
                c++;    //c = 1
                if(c == aaa.length){
                    c = 0;  
                };
                console.log(c);
                // 当自身显示时,兄弟元素隐藏
                aaa.eq(c).show().siblings(".aaa").hide();

                // 字符串值无法创建动画(比如:background-color:red)
                $("#nav p").eq(c).animate({width:"40px"}, 500).css({backgroundColor:"blue"})
                .siblings("p").css({backgroundColor:"#ccc", width:"20px"});
            };


            // 定义单击小圆点切换效果
            function danJiBoFang(obj){
                // 获取当前单击对象的下标值
                c = obj.index();
                // 当自身显示时,兄弟元素隐藏
                aaa.eq(c).show().siblings(".aaa").hide();

                // 字符串值无法创建动画(比如:background-color:red)
                $("#nav p").eq(c).animate({width:"40px"}, 500).css({backgroundColor:"blue"})
                .siblings("p").css({backgroundColor:"#ccc", width:"20px"});
            };

            // 定义单击小圆点事件
            $("#nav p").click(function(){
                danJiBoFang($(this));
            })

            // 定时器,每2秒执行一次
            var time = setInterval(gunDongBoFang, 2000);

            // 鼠标移入#box元素中,停止定时器
            $("#box").mouseenter(function(){
                clearInterval(time);
            });    

            // 鼠标移出#box元素后,开始定时器
            $("#box").mouseleave(function(){
                //鼠标移出时,开始定时器,并将定时器赋值给time变量,而当鼠标再次移入时,则刚好删除上次的定时器,不会累加
                time = setInterval(gunDongBoFang, 2000);
            });    
        });
    </script>

</head>
<body>
    <div id="box">
        <div class="aaa" id="div1">0</div>
        <div class="aaa" id="div2">1</div>
        
        <!-- 图片下方两个小圆点 -->
        <div id="nav">
            <p class="active"></p>
            <p></p>
        </div>
    </div>
</body>
</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>jQuery图片自动切换</title> <script language="javascript" src="http://www.vihchina.net/Public/Js/jQuery/jQuery.js"></script> <style type="text/css"> *{ margin:0; padding:0; border:0; list-style:none} img{ display:block; border:0} body{ padding:50px;} .switch{ width:320px; height:280px; border:1px #CCCCCC solid; padding:3px;} .switch .pic_box{width:320px; height:255px; position:relative;} .switch .pic_box li{ width:320px; height:255px; float:left; position:absolute} .switch .pic_box li .pic{ width:320px; height:255px;} .switch .pic_box li .pic img{ width:320px; height:230px;} .switch .pic_box li .txt{ display:block; width:320px; height:25px; font-size:13px; overflow:hidden; line-height:25px; text-align:center; overflow:hidden; color:#666666} .switch .btn_box{ width:320px; height:25px; float:right; background:#2B2B2B} .switch .btn_box li{ border:1px solid #333333; width:23px; height:23px; float:left; background:#CCCCCC; cursor:pointer; line-height:25px; overflow:hidden; text-align:center; font-weight:700} </style> </head> <body> <div class="switch"> <ul class="pic_box"> <li id="pic1"><a href="http://www.vihchina.net" target="_blank" class="pic"><img src="images/1.jpg" /></a><a href="http://www.vihchina.net" target="_blank" class="txt">京津渝等28城市获批用公积金建保障房</a></li> <li id="pic2" style="display:none"><a href="http://www.vihchina.net" target="_blank" class="pic"><img src="images/2.jpg" /></a><a href="http://www.vihchina.net" target="_blank" class="txt">俄大火逼近绝密核设施 毒雾笼罩莫斯科</a></li> <li id="pic3" style="display:none"><a href="http://www.vihchina.net" target="_blank" class="pic"><img src="images/3.jpg" /></a><a href="http://www.vihchina.net" target="_blank" class="txt">银监会清查银行3000项收费 叫停点钞费</a></li> <li id="pic4" style="display:none"><a href="http://www.vihchina.net" target="_blank" class="pic"><img src="images/4.jpg" /></a><a href="http://www.vihchina.net" target="_blank" class="txt">鸭绿江干流现超警戒洪水 东北强降雨减弱 </a></li> <li id="pic5" style="display:none"><a href="http://www.vihchina.net" target="_blank" class="pic"><img src="images/5.jpg" /></a><a href="http://www.vihchina.net" target="_blank" class="txt">美国防部确定华盛顿号航母将参加黄海军演</a></li> </ul> <ul class="btn_box"> <li style="background:#FF0000">1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> </div> </body> </html> <script language="javascript" type="text/javascript"> $(document).ready(function(){ $(".btn_box li").css('backgroundColor','#eeeeee'); $(".btn_box li").eq(0).css('backgroundColor','#FF0000'); //获取图片总数 var len = $(".btn_box li").size(); //设置默认显示图片 var index = 1; $(".btn_box li").click(function(){ index = $(".btn_box li").index(this); showimg(index); }); // $(".btn_box").hover(function(){ if (myTime){ clearInterval(myTime); } },function(){ myTime = setInterval(function(){ index++; if(index>=len){ index=0; } showimg(index); },4000); }); //自动开始切换 var myTime = setInterval(function(){ showimg(index); index++; if (index>=len){ index=0; } },4000); }); //显示图片 function showimg(i){ $(".btn_box li").css('backgroundColor','#eeeeee'); $(".btn_box li").eq(i).css('backgroundColor','#FF0000'); $(".pic_box li").fadeOut(800); $("#pic"+$(".btn_box li").eq(i).text()).fadeIn(800); } </script> 来源www.vihchina.net
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值