账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    我想实现一个页面中的放大镜效果,居中显示,但交互有问题?
    47
    0

    项目是用angular做的

    <div  class\="wrapper"\>
    
    <div  id\="small"  style\="float: left; margin-right: 10px;"\>
    
    <img  src\="../../assets/images/1.png"\>
    
    <p  id\="move"\></p\>
    
    </div\>
    
    <div  id\="big"  style\="float: left;"\>
    
    <img  src\="../../assets/images/1.png"  id\="big\_img"\>
    
    </div\>
    
    </div\>
    .wrapper {
    
    overflow: auto;
    
    position: absolute;
    
    left: 50%;
    
    top: 50%;
    
    transform: translate(\-50%, \-50%);
    
    white-space: nowrap;
    
    }
    
      
    
    #small {
    
    width: 300px;
    
    height: 200px;
    
    border-radius: 4px;
    
    position: relative;
    
    }
    
      
    
    #small  img {
    
    width: 100%;
    
    height: 100%;
    
    display: block;
    
    }
    
      
    
    #big {
    
    width: 300px;
    
    height: 200px;
    
    overflow: hidden;
    
    position: relative;
    
    border: 1px  solid  #eee;
    
    border-radius: 4px;
    
    display: none;
    
    }
    
      
    
    #big\_img {
    
    position: absolute;
    
    left: 0px;
    
    top: 0px;
    
    }
    
      
    
    #move {
    
    width: 100px;
    
    height: 100px;
    
    background: #000;
    
    opacity: .5;
    
    position: absolute;
    
    display: none;
    
    left: 0px;
    
    top: 0px;
    
    }
    let  move = document.getElementById('move');
    
    let  small = document.getElementById('small');
    
    let  big = document.getElementById('big');
    
    let  big\_img = document.getElementById('big\_img');
    
    small.onmousemove = function(event){
    
    // let \_event = event || window.event;
    
    small.style.cursor = 'move';
    
    // 计算move移动块的left值
    
    let  move\_left = event.clientX - small.offsetLeft - move.offsetWidth/2;
    
    // 计算move移动块的top值
    
    let  move\_top = event.clientY - small.offsetTop - move.offsetHeight/2;
    
    // 超出左边界赋值为0
    
    move\_left = move\_left < 0 ? 0 : move\_left;
    
    // 超出右边界赋值为盒子宽度-移动块的宽度
    
    move\_left = move\_left > small.offsetWidth - move.offsetWidth ? small.offsetWidth - move.offsetWidth : move\_left;
    
    // 超出上边界赋值为0
    
    move\_top = move\_top < 0 ? 0 : move\_top;
    
    // 超出下边界赋值为盒子高度-移动块高度
    
    move\_top = move\_top > small.offsetHeight - move.offsetHeight ? small.offsetHeight - move.offsetHeight : move\_top;
    
    // 修改移动块left、top值
    
      
    
    move.style.left = move\_left + 'px';
    
    move.style.top = move\_top + 'px';
    
    /\*
    
    计算图片需要移动的坐标
    
      
    
    距离左边left 图片宽度 盒子宽度 距离左边left 图片宽度 盒子宽度
    
    big\_x/(big\_img.offsetWidth-big.offsetWidth) = move\_left/(small.offsetWidth-move.offsetWidth);
    
      
    
    big\_y/(big\_img.offsetHeight-big.offsetHeight) = move\_top/(small.offsetHeight-move.offsetHeight);
    
      
    
    \*/
    
      
    
    let  big\_x = move\_left/(small.offsetWidth\-move.offsetWidth) \* (big\_img.offsetWidth\-big.offsetWidth);
    
    let  big\_y = move\_top/(small.offsetHeight\-move.offsetHeight) \* (big\_img.offsetHeight\-big.offsetHeight);
    
    // 修改图片定位
    
    big\_img.style.left = -big\_x + 'px';
    
    big\_img.style.top = -big\_y + 'px';
    
    }
    
      
    
    small.onmouseover = function(){
    
    move.style.display = 'block';
    
    big.style.display = 'block';
    
    }
    
      
    
    small.onmouseout = function(){
    
    move.style.display = 'none';
    
    big.style.display = 'none';
    
    }

    如果将.wrapper那一层div去掉就正常了,但要求是垂直居中显示

    1
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    • o0地狱急诊室〆 普通会员 1楼

      实现页面中的放大镜效果,通常需要结合JavaScript和CSS来完成。以下是一个基础的实现思路:

      HTML结构: ```html

      Small Image

      CSS样式:css .container { position: relative; } .small-image { display: inline-block; } .magnifier-container { position: absolute; top: 0; left: 0; width: 100%; height: 100%; overflow: hidden; } .magnifier-glass { position: absolute; border: 2px solid #ccc; border-radius: 50%; cursor: move; / Set the size of your magnifying glass / width: 200px; height: 200px; } JavaScript交互(使用jQuery示例):javascript var smallImage = $('#small-image'); var magnifierGlass = $('.magnifier-glass'); var magRatio = 3; // 放大倍数

      magnifierGlass.on('mousemove', function(e) { var parentOffset = $(this).parent().offset(); var x = e.pageX - parentOffset.left; var y = e.pageY - parentOffset.top;

      // 防止超出小图边界 x = Math.max(0, x); y = Math.max(0, y); x = Math.min(x, smallImage.width() - magnifierGlass.width()); y = Math.min(y, smallImage.height() - magnifierGlass.height());

      magnifierGlass.css({ left: x, top: y });

      // 更新大图的位置和尺寸以模拟放大效果 var largeImg = new Image(); largeImg.src = smallImage.attr('src'); var放大Width = smallImage.width() * magRatio; var放大Height = smallImage.height() * magRatio; var destX = -(x * magRatio - magnifierGlass.width() / 2); var destY = -(y * magRatio - magnifierGlass.height() / 2);

      $('.magnifier-container').css({ 'background-image': 'url("' + largeImg.src + '")', 'background-position': ${destX}px ${destY}px, 'background-size': ${放大Width}px ${放大Height}px }); }); ``` 这个例子中,鼠标在放大镜内移动时,会根据鼠标位置实时改变大图的背景位置,从而达到放大镜的效果。请注意,这只是一个基本的实现方式,可能需要根据你的实际需求进行调整。

      同时,请确保你已经在项目中引入了jQuery库。

    更多回答
    扫一扫访问手机版