jQuery实现产品图放大

jquery 代码

$(document).ready(function() {
    var index = 0;
    $(".but li").mouseover(function() {
        $(this).addClass("once").siblings().removeClass("once");
        index = $(this).index();
        $(".probox img").attr('src', $(this).attr('data'))
        $(".showbox img").attr('src', $(this).attr('kdata'));
    });

    function Zoom(imgbox, hoverbox, l, t, x, y, h_w, h_h, probox) {
        var moveX = x - l - (h_w / 2);
        //鼠标区域距离
        var moveY = y - t - (h_h / 2);
        //鼠标区域距离
        if (moveX < 0) {
            moveX = 0
        }
        if (moveY < 0) {
            moveY = 0
        }
        if (moveX > imgbox.width() - h_w) {
            moveX = imgbox.width() - h_w
        }
        if (moveY > imgbox.height() - h_h) {
            moveY = imgbox.height() - h_h
        }
        //判断鼠标使其不跑出图片框
        var zoomX = probox.width() / imgbox.width()
        //求图片比例
        var zoomY = probox.height() / imgbox.height()

        probox.css({
            left: -(moveX * zoomX),
            top: -(moveY * zoomY)
        })
        hoverbox.css({
            left: moveX,
            top: moveY
        })
        //确定位置

    }
    $(".tablea").find(".box:first").show(); //为每个BOX的第一个元素显示        
    $("#oranger a").on("mouseover", function() { //给a标签添加事件  
        var index = $(this).index(); //获取当前a标签的个数  
        $(this).parent().next().find(".box").hide().eq(index).show(); //返回上一层,在下面查找css名为box隐藏,然后选中的显示  
        $(this).addClass("hover").siblings().removeClass("hover"); //a标签显示,同辈元素隐藏  
    })

    function Zoomhover(imgbox, hoverbox, probox) {
        var l = imgbox.offset().left;
        var t = imgbox.offset().top;
        var w = hoverbox.width();
        var h = hoverbox.height();
        var time;
        $(".showbox img,.hoverbox").mouseover(function(e) {
            var x = e.pageX;
            var y = e.pageY;
            $(".hoverbox,.probox").show();
            hoverbox.css("opacity", "0.3")
            time = setTimeout(function() {
                Zoom(imgbox, hoverbox, l, t, x, y, w, h, probox)
            }, 1)
        }).mousemove(function(e) {
            var x = e.pageX;
            var y = e.pageY;
            time = setTimeout(function() {
                Zoom(imgbox, hoverbox, l, t, x, y, w, h, probox)
            }, 1)
        }).mouseout(function() {
            probox.parent().hide()
            hoverbox.hide();
        })
    }
    $(function() {
        Zoomhover($(".showbox img"), $(".hoverbox"), $(".probox img"));
    })
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值