原生js实现放大镜效果

痛定思痛。 2024-04-18 23:23 167阅读 0赞

今天是中秋佳节,在这了我祝大家中秋快乐,心想事成,工作顺利哈!

放大镜效果在电商网站上较为常见的 一个效果,主要针对鼠标在图片选取部分放大查看。效果如图所示
在这里插入图片描述

分析页面可知,页面主要分为三个部分,左侧图片缩略图,鼠标滑动版,右侧原图。

HTML

两个div搞定。s_box为缩略图,b_box右侧为原图,span为鼠标移动时的放大区域。

  1. <div class="s_box">
  2. <img src="img/img1.jpg" alt="">
  3. <span></span>
  4. </div>
  5. <div class="b_box">
  6. <img src="img/img1.jpg" alt="">
  7. </div>

CSS

要点 定位问题,通过位置,显示右侧原图的部分区域,所谓的放大并不是真的放大,而是只呈现了原图的部分区域。

  1. .s_box,
  2. .b_box {
  3. width: 400px;
  4. height: 300px;
  5. position: absolute;
  6. top: 100px;
  7. }
  8. .s_box {
  9. left: 50px;
  10. }
  11. .s_box img {
  12. width: 400px;
  13. height: 300px;
  14. }
  15. .s_box span {
  16. position: absolute;
  17. left: 0;
  18. top: 0;
  19. background: rgba(200, 200, 200, 0.5);
  20. display: none;
  21. }
  22. .b_box {
  23. left: 500px;
  24. display: none;
  25. overflow: hidden;
  26. }
  27. .b_box img {
  28. width: 1200px;
  29. height: 900px;
  30. position: absolute;
  31. left: 0;
  32. top: 0;
  33. }

JS

js 控制滑块位置,及右侧图片显示区域。

这里我是用的是面向对象编程,不知为啥特别钟爱面向对象编程

  1. function Magnifier(){
  2. // 1.选元素
  3. this.sBox = document.querySelector(".s_box");
  4. this.span = document.querySelector(".s_box span");
  5. this.bBox = document.querySelector(".b_box");
  6. this.bImg = document.querySelector(".b_box img");
  7. // 2.绑定事件
  8. this.init()
  9. }
  10. Magnifier.prototype.init = function(){
  11. var that = this;
  12. // 进入
  13. this.sBox.onmouseover = function(){
  14. // 3-1.显示,计算span的宽高
  15. that.show()
  16. }
  17. // 离开
  18. this.sBox.onmouseout = function(){
  19. // 3-2.隐藏
  20. that.hide()
  21. }
  22. // 移动
  23. this.sBox.onmousemove = function(eve){
  24. var e = eve || window.event;
  25. // 5.span跟随鼠标
  26. that.move(e)
  27. }
  28. }
  29. Magnifier.prototype.show = function(){
  30. // 显示,计算span的宽高
  31. this.span.style.display = "block";
  32. this.bBox.style.display = "block";
  33. this.span.style.width = this.bBox.offsetWidth / this.bImg.offsetWidth * this.sBox.offsetWidth + "px";
  34. this.span.style.height = this.bBox.offsetHeight / this.bImg.offsetHeight * this.sBox.offsetHeight + "px";
  35. }
  36. Magnifier.prototype.hide = function(){
  37. // 隐藏
  38. this.span.style.display = "none";
  39. this.bBox.style.display = "none";
  40. }
  41. Magnifier.prototype.move = function(e){
  42. // 计算移动的距离
  43. var l = e.clientX - this.sBox.offsetLeft - this.span.offsetWidth/2;
  44. var t = e.clientY - this.sBox.offsetTop - this.span.offsetHeight/2;
  45. // 边界限定
  46. if(l<0) l=0;
  47. if(t<0) t=0;
  48. if(l>this.sBox.offsetWidth - this.span.offsetWidth){
  49. l=this.sBox.offsetWidth - this.span.offsetWidth
  50. }
  51. if(t>this.sBox.offsetHeight - this.span.offsetHeight){
  52. t=this.sBox.offsetHeight - this.span.offsetHeight
  53. }
  54. // span跟随鼠标
  55. this.span.style.left = l + "px";
  56. this.span.style.top = t + "px";
  57. // 计算比例
  58. // 当前值 / 总值,得到的就是比例
  59. var x = l / (this.sBox.offsetWidth - this.span.offsetWidth);
  60. var y = t / (this.sBox.offsetHeight - this.span.offsetHeight);
  61. // 根据比例计算右边大图应该移动的距离
  62. // 比例 * 总值,得到的就是当前应该移动的距离
  63. this.bImg.style.left = x * (this.bBox.offsetWidth - this.bImg.offsetWidth) + "px";
  64. this.bImg.style.top = y * (this.bBox.offsetHeight - this.bImg.offsetHeight) + "px";
  65. }
  66. new Magnifier();

就这样,放大镜效果就实现啦!

最后再祝大家节日快乐,每天都要开心噢!

发表评论

表情:
评论列表 (有 0 条评论,167人围观)

还没有评论,来说两句吧...

相关阅读

    相关 原生js实现放大镜效果

    今天是中秋佳节,在这了我祝大家中秋快乐,心想事成,工作顺利哈! 放大镜效果在电商网站上较为常见的 一个效果,主要针对鼠标在图片选取部分放大查看。效果如图所示 ![...

    相关 jQuery实现放大镜效果

    1.1.1 摘要 相信大家都见过或使用过放大镜效果,甚至实现过该效果,它一般应用于放大查看商品图片,一些电商网站(例如:凡客,京东商城,阿里巴巴等)都有类似

    相关 js实现购物网站放大镜效果

    > 在一些主流的购物网站上,我们经常能看到一些带有放大镜效果的图片框,看似神奇,指哪哪大,其实实现的原理很简单 主要原理:实现放大镜的核心是需要有三套大小不同,形状相同的图片