js图片点击放大之后左右切换效果

要实现点击放大不要切换效果请转至:https://blog.csdn.net/white1114579650/article/details/111941631

效果如下

核心HTML代码

<img style="width: 150px;height: 150px;padding: 10px;" src="img/微信图片_20201230091744.jpg" class="img-responsive">
<img style="width: 150px;height: 150px;padding: 10px;" src="img/微信图片_20201230091800.jpg" class="img-responsive">
<div class="gray"  style="position:fixed;top:0;left:0;background:rgba(0,0,0,0.7);z-index:2;width:100%;height:100%;display:none;"></div>
<div class="showImg"  style="position:absolute;z-index: 10;display: none;">
    <img src="img/right.png" class="prev" style="position: absolute;margin-left: -90px;width: 100px;height: 100px;text-align: center;">
    <img src="img/left.png" class="next" style="position: absolute;margin-left: 390px;width: 100px;height: 100px;text-align: center;">
    <img src="#"  style="border:5px solid #fff;" class="bigImg">
</div>

核心js代码

var index=0;
var bigSrc="";

$(".img-responsive").click(function() {
    index = $(this).index();
    $(".gray").show();
    $(".showImg").show();
    bigSrc = $(this).attr("src");
    $(".showImg img.bigImg").attr("src", bigSrc).on('load',function() {
        var windowW = $(window).width() var windowH = $(window).height();
        var realWidth = this.width;
        var readHeight = this.height;
        var imgWidth, imgHeight;
        var scale = 0.3;
        if (realWidth > windowW + scale) {
            imgHeight = windowH * scale;
            imgWidth = imgHeight / readHeight * realWidth;
            if (imgWidth > windowW * scale) {
                imgWidth = windowW * scale;
            }
        } else if (realWidth > windowW * scale) {
            imgWidth = windowW * scale;
            imgHeight = imgWidth / realWidth * readHeight;
        } else {
            imgWidth = realWidth;
            imgHeight = readHeight;
        }
        $(".showImg img.bigImg").css("width", imgWidth);

        $(".prev").css("margin-top", $(".bigImg").height() / 2 - 50);
        $(".next").css("margin-top", $(".bigImg").height() / 2 - 50);
        var w = (windowW - imgWidth) / 2;
        var h = (windowH - imgHeight) / 2;
        $(".showImg").css({
            "top": h,
            "left": w
        });
    });

});
$(".gray").click(function() {
    $(".gray").hide();
    $(".showImg").hide();
});
$(".showImg img.next").click(function() {
    index++;
    if (index > $(".img-responsive").length - 1) {
        index = 0;
    }
    bigSrc = $(".img-responsive").eq(index).attr("src");
    $(".showImg img.bigImg").attr("src", bigSrc);
});
$(".showImg img.prev").click(function() {
    index--;
    if (index < 0) {
        index = $(".img-responsive").length - 1;
    }
    bigSrc = $(".img-responsive").eq(index).attr("src");
    $(".showImg img.bigImg").attr("src", bigSrc);

});

注:必须引入jquery文件!!!
 

要实现点击放大不要切换效果请转至:https://blog.csdn.net/white1114579650/article/details/111941631

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值