【JQuery】点左右箭头,轮换图片展示

HTML
<div class="photos_l"><img alt="" src="/images/arrow_l.png" /></div>
<div class="photos">
  <div class="kkkk">
    <div class="kkk">
       <asp:Repeater ID="rptImages" runat="server">
         <ItemTemplate>
            <div class="kk">
               <a href='detail.aspx?p_id=<%#Eval("P_ID") %>'><img alt="" src="<%#Eval("P_Images") %>" /></a>
            </div>
        </ItemTemplate>
      </asp:Repeater>
    </div>
  </div>
</div>
<div class="photos_r"><img alt="" src="/images/arrow_r.png" /></div>

CSS
.list_style1 .photos_l {position:absolute; top:655px; left:18px; cursor:pointer;}
.list_style1 .photos_r {position:absolute; top:655px; right:18px; cursor:pointer;}
.list_style1 .photos {position:absolute; top:535px; left:50px; width:905px; height:303px; overflow:hidden;}
.list_style1 .photos .kkkk{ position:relative; width:99999px;}
.list_style1 .photos .kkk{float:left;}
.list_style1 .photos .kk{float:left; width:170px; height:260px; padding:0px 5px; }


JS

$(window).load(function () { //不用$(document).ready()是为了在数据加载完全后才能把总宽度测量正确
    $(".photos_l").css({ "display": "none" });
    $(".photos_r").css({ "display": "none" });
    list_style1和list_style2的照片左右滚动
    $(".list_style1,.list_style2").one("mouseenter", function () {
        var la = $(this).children(".photos_l");
        var ra = $(this).children(".photos_r");
        var PW = $(this).children(".photos").width();//测得放照片的容器宽度
        var kkkk = $(this).children(".photos").children(".kkkk");
        var kkk = $(this).children(".photos").children(".kkkk").children(".kkk");
        if (kkk.width() > PW) {
            var n = 0;
            ra.css({ "display": "block" });
            ra.click(function () {
                la.css({ "display": "block" });
                var xx = kkkk.offset().left - kkk.offset().left;
                if (xx == PW * n || xx == PW * n - 1) { //这里有个或,是因为IE8测量的宽度会短1个像素
                    kkk.animate({ marginLeft: "-" + PW * (n + 1) + "px" }, function () {
                        if (kkk.width() <= PW * (n + 2)) {
                            ra.css({ "display": "none" });
                        }
                        n = n + 1;
                    });
                }
            });
            la.click(function () {
                ra.css({ "display": "block" });
                var xx = kkkk.offset().left - kkk.offset().left;
                if (xx == PW * n || xx == PW * n - 1) {
                    kkk.animate({ marginLeft: "-" + PW * (n - 1) + "px" }, function () {
                        if (n <= 1) {
                            la.css({ "display": "none" });
                        }
                        n = n - 1;
                    });
                }
            });
        }
    });
});



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用jQuery实现左右箭头图片轮播的切换,代码如下: HTML部分: ``` <div class="slider"> <ul class="slider-list"> <li><img src="img1.jpg"></li> <li><img src="img2.jpg"></li> <li><img src="img3.jpg"></li> </ul> <a class="slider-arrow slider-prev" href="javascript:void(0);"><</a> <a class="slider-arrow slider-next" href="javascript:void(0);">></a> </div> ``` CSS部分: ``` .slider { position: relative; width: 500px; height: 300px; overflow: hidden; } .slider-list { position: absolute; left: 0; top: 0; width: 1500px; height: 300px; margin: 0; padding: 0; } .slider-list li { float: left; list-style: none; width: 500px; height: 300px; } .slider-arrow { position: absolute; top: 50%; margin-top: -20px; width: 40px; height: 40px; line-height: 40px; text-align: center; font-size: 24px; color: #fff; background-color: rgba(0, 0, 0, 0.5); text-decoration: none; border-radius: 50%; } .slider-prev { left: 20px; } .slider-next { right: 20px; } ``` jQuery部分: ``` $(function() { var sliderList = $('.slider-list'); var sliderItem = $('.slider-list li'); var sliderCount = sliderItem.length; var sliderWidth = sliderItem.width(); var sliderHeight = sliderItem.height(); var sliderArrow = $('.slider-arrow'); var currentIndex = 0; sliderList.width(sliderWidth * sliderCount); sliderArrow.on('click', function() { var direction = $(this).hasClass('slider-prev') ? -1 : 1; currentIndex += direction; if (currentIndex === -1) { currentIndex = sliderCount - 1; } else if (currentIndex === sliderCount) { currentIndex = 0; } sliderList.animate({left: -sliderWidth * currentIndex}, 500); }); }); ``` 以上代码实现了一个简单的左右箭头图片轮播,击左箭头向左切换图片击右箭头向右切换图片。注意要将图片的宽度和高度设置好,使得图片排列在一行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值