首页 > 其他分享 >放大镜效果的简单实现

放大镜效果的简单实现

时间:2022-08-20 18:46:34浏览次数:41  
标签:smallBox style 效果 放大镜 move bigBox 简单 bigImg targetPoint

class Magnifier {
    constructor(smallBox, bigBox) {
        this.smallBox = smallBox
        this.bigBox = bigBox
        this.move = smallBox.querySelector('.move')
        this.bigImg = bigBox.children[0]
        this.init()
        this.handlerMouse()
    }
    init() {
        //计算对应的move这个盒子的宽高
        // 大的比大的等于小的比小的 bigImg/bigBox = box/move ==>  bigImg/box == bigBox/move
        this.move.style.width = this.smallBox.clientWidth / (this.bigImg.clientWidth / this.bigBox
                                                             .clientWidth) + 'px'
        this.move.style.height = this.smallBox.clientHeight / (this.bigImg.clientHeight / this.bigBox
                                                               .clientHeight) + 'px'
        //先需要隐藏
        this.move.style.display = 'none'
        this.bigBox.style.display = 'none'
    }
    handlerMouse() {
        //移入移出
        this.smallBox.onmouseenter = () => {
            this.move.style.display = 'block'
            this.bigBox.style.display = 'block'
        }
        this.smallBox.onmouseleave = () => {
            this.move.style.display = 'none'
            this.bigBox.style.display = 'none'
        }
        //移动
        this.smallBox.onmousemove = ({
            pageX,
            pageY
        }) => {
            //获取鼠标在smallbox里面位置
            let currentX = pageX - this.smallBox.offsetLeft
            let currentY = pageY - this.smallBox.offsetTop
            //中心点位置
            let centerPoint = {
                x: this.smallBox.clientWidth / 2,
                y: this.smallBox.clientHeight / 2
            }
            //移动的位置
            let targetPoint = {
                x: currentX - centerPoint.x,
                y: currentY - centerPoint.y
            }
            //边界判断
            if(targetPoint.x<0){
                targetPoint.x = 0
            }
            if(targetPoint.y<0){
                targetPoint.y = 0
            }
            //最大值判断
            let maxPoint = {
                x: this.smallBox.clientWidth - this.move.offsetWidth,
                y: this.smallBox.clientHeight - this.move.offsetHeight
            }
            if(targetPoint.x > maxPoint.x){
                targetPoint.x = maxPoint.x
            }
            if(targetPoint.y > maxPoint.y){
                targetPoint.y = maxPoint.y
            }
            //设置对应的位置
            this.move.style.left =  targetPoint.x + 'px'
            this.move.style.top =  targetPoint.y + 'px'
            //还要设置大盒子里面图片的位置
            this.bigImg.style.left = -targetPoint.x * this.bigImg.clientWidth / this.smallBox.clientWidth + 'px'
            this.bigImg.style.top = -targetPoint.y * this.bigImg.clientHeight / this.smallBox.clientHeight + 'px'
        }
    }
}
var small = document.querySelector('.box')
var big = document.querySelector('.bigbox')
new Magnifier(small, big)

标签:smallBox,style,效果,放大镜,move,bigBox,简单,bigImg,targetPoint
From: https://www.cnblogs.com/gujmnlk/p/16608374.html

相关文章

  • jQuery实现简单弹窗遮罩效果
    https://www.jb51.net/article/106880.htm效果图:图(1)初始图图(2)点击按钮后代码如下:1234567891011121314151617181920212223242526......
  • RabbitMQ安装及简单使用
    1.rabbitmq启动rabbitmq路径:/usr/local/rabbitmq_server-3.7.8在rabbitmq安装目录下,进入到sbin目录,执行:#启动rabbitmqrabbitmq-server./rabbitmq-server2.rabbitmq......
  • 使用docker简单编译k20pro内核
    简介本文将介绍一下如何使用docker编译红米k20pro的内核。作者当时尝试构建内核的原因是为了将3年前(好像是吧)购买的k20pro至尊版(已退役,12GB内存,512GB硬盘)制作成一个小的服......
  • 明星效应。很简单,在一个领域保持顶尖水平,比在一两个领域保持领先水平和五六个领域保持
    明星效应。很简单,在一个领域保持顶尖水平,比在一两个领域保持领先水平和五六个领域保持一般水准都要更有价值、并且收益更好。有悖常识的真相:让未来更开放的方式,正是专注......
  • JPA 入门实战(2)--简单使用
    本文主要介绍JPA的实际使用,相关的环境及软件信息如下:JPA2.2(eclipselink2.7.10、hibernate-entitymanager5.6.10.Final、openjpa3.2.2),JPA3.0(eclipselink3.0.2、h......
  • 简单工厂模式
    1定义简单工厂模式:简单工厂模式又称作静态方法模式(因为工厂中定义了一个静态方法)工厂类负责生产出各类的产品。其实就是根据传入的参数的不同来生产出不同的产品。......
  • 事件循环:微任务和宏任务在v8中实现的简单理解
    微任务在js中,当使用promise,会将当前任务加入事件执行的微任务队列,有且只有这一种方法可以,因为当使用了promise,在JS引擎中会触发VM::queueMicrotask,会向m_microtaskQueue队......
  • elasticsearch中使用curl进行的简单查询
    curl:-X:指定http的请求方式,有HEAD、GET、POST、PUT、DELETE-d:指定要传输的数据-H:指定http的请求头信息curl-XPUThttp://ip:port/索引名?pretty--创建索引cur......
  • CSS-盒子四周阴影效果
    CSS-盒子四周阴影效果属性box-shadow,<styletype="text/css">.fei{margin-left:230px;width:100px;height:100px;border:1pxsolid#eee;......
  • flutter 效果实现 —— sliver 固定
    效果:说明:绿色块在向上滑动,距离顶部103的高度(即AppBar下面)时固定代码:classPinnedSliverPageextendsStatefulWidget{constPinnedSliverPage({Key?key}):......