html - 放大图像的特定区域

标签 html css

有没有办法引导缩放过渡放大图像的顶部或底部或任何其他特定区域?我目前正在这样做:

.grow { 
     transition: all 1s ease-in-out; 
}
.grow:hover { 
     transform: scale(8); 
}
<img src="http://placehold.it/350x150" class="grow"/>

我将如何对此进行扩展?

最佳答案

好的,首先,我从这个 website 中获取了代码.

快进到教程结尾,您将获得代码。

代码

$(document).ready(function() {

  var native_width = 0;
  var native_height = 0;

  $(".magnify").mousemove(function(e) {
    if (!native_width && !native_height) {
      var image_object = new Image();
      image_object.src = $(".small").attr("src");

      native_width = image_object.width;
      native_height = image_object.height;
    } else {
      var magnify_offset = $(this).offset();
      var mx = e.pageX - magnify_offset.left;
      var my = e.pageY - magnify_offset.top;

      if (mx < $(this).width() && my < $(this).height() && mx > 0 && my > 0) {
        $(".large").fadeIn(100);
      } else {
        $(".large").fadeOut(100);
      }
      if ($(".large").is(":visible")) {
        var rx = Math.round(mx / $(".small").width() * native_width - $(".large").width() / 2) * -1;
        var ry = Math.round(my / $(".small").height() * native_height - $(".large").height() / 2) * -1;
        var bgp = rx + "px " + ry + "px";

        var px = mx - $(".large").width() / 2;
        var py = my - $(".large").height() / 2;
        $(".large").css({
          left: px,
          top: py,
          backgroundPosition: bgp
        });
      }
    }
  })
})
* {
  margin: 0;
  padding: 0;
}
.magnify {
  width: 200px;
  margin: 50px auto;
  position: relative;
}
.large {
  width: 175px;
  height: 175px;
  position: absolute;
  border-radius: 100%;
  box-shadow: 0 0 0 7px rgba(255, 255, 255, 0.85), 0 0 7px 7px rgba(0, 0, 0, 0.25), inset 0 0 40px 2px rgba(0, 0, 0, 0.25);
  background: url('http://thecodeplayer.com/uploads/media/iphone.jpg') no-repeat;
  display: none;
}
.small {
  display: block;
}
* {
  margin: 0;
  padding: 0;
}
.magnify {
  width: 200px;
  margin: 50px auto;
  position: relative;
}
.large {
  width: 175px;
  height: 175px;
  position: absolute;
  border-radius: 100%;
  box-shadow: 0 0 0 7px rgba(255, 255, 255, 0.85), 0 0 7px 7px rgba(0, 0, 0, 0.25), inset 0 0 40px 2px rgba(0, 0, 0, 0.25);
  background: url('http://thecodeplayer.com/uploads/media/iphone.jpg') no-repeat;
  display: none;
}
.small {
  display: block;
}
<div class="magnify">

  <div class="large"></div>
  <img class="small" src="http://thecodeplayer.com/uploads/media/iphone.jpg" width="200" />

</div>

<script src="http://thecodeplayer.com/uploads/js/prefixfree.js" type="text/javascript"></script>
<script src="http://thecodeplayer.com/uploads/js/jquery-1.7.1.min.js" type="text/javascript"></script>

关于html - 放大图像的特定区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39176956/

相关文章:

java - Selenium - 跳过一张 table

html - 导航栏使用 ul 缩放不正确

html - 无序列表和有序列表未与列对齐

javascript - 检测文本输入的值是否在没有事件的情况下发生变化

javascript - localStorage 一切,包括在 jQuery 的 div 容器内动态创建的图像

html - 删除电子邮件通讯表中图像之间的空间

html - 联系表混淆

html - CSS 将输入文本包含到 100% 宽度

css - z-index 问题 IE 7

html - css:将颜色混合到现有背景颜色中的类?