晨风牧笛 2019-05-31 09:45 采纳率: 100%
浏览 675

前端jquery 图片自动轮播

我想要实现图片的自动轮播,并且可以点击图片在新页面查看图片,刚开始的一次循环点击图片可以跳转新页面,可是之后的点击却无效

<!doctype

<html>
<head>
<meta charset="UTF-8">
<title>Demo13-2</title>
<style>
    .container{width:300px;height:150px;margin:200px auto;}
    .left,.right{width:10%;height:100%;line-height:150px;text-align: center;float:left}
    .picbox{width:80%;height:100%;float:left;overflow: hidden;}
    .pic{position:relative;width:720px;overflow:visible}
    img{width:100%;height:100%}
    .img{width:240px;height:150px;float:left}
</style>
<script src="js/jquery-3.3.1.js"></script>
<script>
    $(function(){
        var stop =false;
        var index = -1;
        var pics = ["1","2","3"];
        $.each(pics,function(index,value){
            $(".pic").append($("<div class='img'><a href='img/"+value+".jpg'><image src='img/"+value+".jpg' /></a></div>"));
        })
        $(".pic").hover(function(){stop=true;},function(){stop=false;});
        setInterval(function(){
            if(stop) return;
            var left = parseInt($(".pic").css("left"))-1;
            //alert(left);
            $(".pic").css({left:left});
            if(left % 240 ==0)
            {
                index++;
                var w = parseInt($(".pic").css("width")) +240 ;
                $(".pic").css({"width":w})
                $(".pic").append($("<div class='img'><image src='img/"+pics[index%pics.length]+".jpg' /></div>"));
            }
        },10);

    })
</script>
</head>

<body>
    <div class="container">
        <div class="left"></div>
        <div class="picbox">
            <div class="pic"></div>
        </div>
        <div class="right"></div>
    </div>
</body>
</html>


  • 写回答

1条回答

  • 张兴华(MarsXH.Chang) 前端领域新星创作者 2019-05-31 11:18
    关注
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Demo13-2</title>
    <style>
        .container{width:300px;height:150px;margin:200px auto;}
        .left,.right{width:10%;height:100%;line-height:150px;text-align: center;float:left}
        .picbox{width:80%;height:100%;float:left;overflow: hidden;}
        .pic{position:relative;width:720px;overflow:visible}
        img{width:100%;height:100%}
        .img{width:240px;height:150px;float:left}
    </style>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script>
        $(function(){
            var stop =false;
            var index = -1;
            var pics = ["1","2","3"];
            $.each(pics,function(index,value){
                $(".pic").append($("<div class='img'><a href='img/"+value+".jpg'><image src='img/"+value+".jpg' /></a></div>"));
            })
            $(".pic").hover(function(){stop=true;},function(){stop=false;});
            setInterval(function(){
                if(stop) return;
                var left = parseInt($(".pic").css("left"))-1;
                //alert(left);
                $(".pic").css({left:left});
                if(left % 240 ==0)
                {
                    index++;
                    var w = parseInt($(".pic").css("width")) +240 ;
                    $(".pic").css({"width":w})
                    $(".pic").append($("<div class='img'><a href='img/"+pics[index%pics.length]+".jpg'><image src='img/"+pics[index%pics.length]+".jpg' /></a></div>"));
                }
            },10);
    
        })
    </script>
    </head>
    
    <body>
        <div class="container">
            <div class="left"></div>
            <div class="picbox">
                <div class="pic"></div>
            </div>
            <div class="right"></div>
        </div>
    </body>
    </html>
    
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突