layer.photos 图片弹出无法滚轮放大缩写的问题解决

layui的版本问题,在旧版本中下列方式可以实现滚轮放大缩小,但是出现新的业务场景,需要增加评价项,如图。评价项需要引用rate.js,但是旧版本中没有,引用了新版本后,发现照片可以预览,但是滚轮放大缩小失效了
在这里插入图片描述

HTML

<div class="layui-form-item">
     <label class="layui-form-label">故障图片:</label>
     <div class="layui-input-inline" style="margin: 10px" v-for="imageUrl in imageList">
        <img :src="imageUrl" style="width: 150px;height: 150px;" aria-readonly="true"
                             @click="iamgeUrlAmplify">
     </div>
</div>

JS

iamgeUrlAmplify: function (e) {
                var url = e.target.src;
                layer.photos({
                    photos: {"data": [{"src": e.target.src}]},
                });
            },

解决办法
在原JS中加入下面这段代码即可放大缩小

$(document).on("mousewheel",".layui-layer-photos",function(ev){
                            var oImg = this;
                            var ev = event || window.event;//返回WheelEvent
                            //ev.preventDefault();
                            var delta = ev.detail ? ev.detail > 0 : ev.wheelDelta < 0;
                            var ratioL = (ev.clientX - oImg.offsetLeft) / oImg.offsetWidth,
                                ratioT = (ev.clientY - oImg.offsetTop) / oImg.offsetHeight,
                                ratioDelta = !delta ? 1 + 0.1 : 1 - 0.1,
                                w = parseInt(oImg.offsetWidth * ratioDelta),
                                h = parseInt(oImg.offsetHeight * ratioDelta),
                                l = Math.round(ev.clientX - (w * ratioL)),
                                t = Math.round(ev.clientY - (h * ratioT));
                            $(".layui-layer-photos").css({
                                width: w, height: h
                                ,left: l, top: t
                            });
                            $("#layui-layer-photos").css({width: w, height: h});
                            $("#layui-layer-photos>img").css({width: w, height: h});
                        });

完整JS

iamgeUrlAmplify: function (e) {
                var url = e.target.src;
                layer.photos({
                    photos: {"data": [{"src": e.target.src}]},
                    success:function () {
                        $(document).on("mousewheel",".layui-layer-photos",function(ev){
                            var oImg = this;
                            var ev = event || window.event;//返回WheelEvent
                            //ev.preventDefault();
                            var delta = ev.detail ? ev.detail > 0 : ev.wheelDelta < 0;
                            var ratioL = (ev.clientX - oImg.offsetLeft) / oImg.offsetWidth,
                                ratioT = (ev.clientY - oImg.offsetTop) / oImg.offsetHeight,
                                ratioDelta = !delta ? 1 + 0.1 : 1 - 0.1,
                                w = parseInt(oImg.offsetWidth * ratioDelta),
                                h = parseInt(oImg.offsetHeight * ratioDelta),
                                l = Math.round(ev.clientX - (w * ratioL)),
                                t = Math.round(ev.clientY - (h * ratioT));
                            $(".layui-layer-photos").css({
                                width: w, height: h
                                ,left: l, top: t
                            });
                            $("#layui-layer-photos").css({width: w, height: h});
                            $("#layui-layer-photos>img").css({width: w, height: h});
                        });
                    },end: function(){ //销毁回调

                    }
                });

代码来源:前端学习之layui照片查看器缩放

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值