js实现轮播图片,淡入淡出

代码效果:


Paste_Image.png
<!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>Jq切换</title>
<style type="text/css">

/**********总体布局**************/
*{margin:0; padding:0; border:0;}
body{background-color:rgba(0,0,0,0.4);}
#content{position:relative; width:970px; height:550px; margin:20px auto; border:2px solid #FFF;}
#content:hover #tags img{opacity:1;}

/**********图片主要显示区**************/
#player{position:absolute; width:100%; height:100%; top:0; left:0;}
#player img{display:inline-block; position:absolute; float:left; width:970px; height:550px; top:0;}

/**********图片显示标签**************/
#tags{position:absolute; left:30px; bottom:20px;}
#tags img{display:inline-block; width:100px; height:61.8px; float:left; margin-right:10px; transition:1s; opacity:0.5;}
#tags img:hover{transform:scale(1,1); cursor:pointer;}
</style>
<script type="text/javascript" src="http://www.imooc.com/data/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
    $(function(){
        var same=0;                    //用于避免触碰到相同标签式是发生切换            
        var interval=5000;            //图片切换时间
        var timer;                    //计时器

        /*********显示和隐藏图片************/
        $("#player img").hide();
        $("#player img").eq(0).show();
        $("#tags img").mouseover(function(){
            var tags=$("#tags img").index(this);
            if(same!=tags){
                $("#player img").fadeOut();
                $("#player img").eq(tags).fadeIn(500);    
            }
            same=tags;
        })

        /*********自动播放图片***********/
        function autoplay(){
            same++;
            if(same>=$("#tags img").length){
                same=0;    
            }
            $("#player img").fadeOut();
            $("#player img").eq(same).fadeIn(500);    
        }

        /**********播放图片切换**************/
        function play(){
            timer=setInterval(autoplay,interval);    
        }

        /**********停止图片切换**************/
        function stop(){
            clearInterval(timer);    
        }

        /*****鼠标悬浮或移出时播放或停止切换图片***/
        $("#content").hover(function(){
            stop();
        },
        function(){
            play();    
        })

        play();
    })
</script>
</head>

<body>
    <div id="content">
        <!--主显示区-->
        <div id="player">
            ![](http://upload-images.jianshu.io/upload_images/5245297-684b1d86f4c982de.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
            ![](http://upload-images.jianshu.io/upload_images/5245297-5f93d2ce953162f5.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
            ![](http://upload-images.jianshu.io/upload_images/5245297-9051a371f1e84962.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
            ![](http://upload-images.jianshu.io/upload_images/5245297-6b48a9d85f195251.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
            ![](http://upload-images.jianshu.io/upload_images/5245297-6492bb4e3432771f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
            ![](http://upload-images.jianshu.io/upload_images/5245297-8ae1d8728866c3e6.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
            ![](http://img.pconline.com.cn/images/upload/upc/tx/wallpaper/1209/12/c0/13805531_1347419023266.jpg)
        </div>
        <!--显示标签-->
        <div id="tags">
            ![](http://upload-images.jianshu.io/upload_images/5245297-684b1d86f4c982de.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
            ![](http://upload-images.jianshu.io/upload_images/5245297-5f93d2ce953162f5.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
            ![](http://upload-images.jianshu.io/upload_images/5245297-9051a371f1e84962.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
            ![](http://upload-images.jianshu.io/upload_images/5245297-6b48a9d85f195251.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
            ![](http://upload-images.jianshu.io/upload_images/5245297-6492bb4e3432771f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
            ![](http://upload-images.jianshu.io/upload_images/5245297-8ae1d8728866c3e6.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
            ![](http://img.pconline.com.cn/images/upload/upc/tx/wallpaper/1209/12/c0/13805531_1347419023266.jpg)
        </div>
    </div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值