【js-实战篇】放大镜

实现效果,如图所示:

 主要用到了事件绑定,和位置计算的几个常用属性,clientX,clientY,offsetTop.offsetLeft(和定位配合使用)

下面是代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .small {
            position: relative;
            width: 350px;
            height: 350px;
            border: 1px solid #ccc;
            float: left;
            background-clip: padding-box;
            cursor: pointer;
        }
        
        .big {
            position: relative;
            width: 540px;
            height: 540px;
            border: 1px solid #ccc;
            float: left;
            margin-left: 10px;
            background-clip: padding-box;
            overflow: hidden;
            display: none;
        }
        
        .big>img {
            position: absolute;
        }
        
        .small .move {
            position: absolute;
            top: 0;
            left: 0;
            background: rgba(200, 200, 50, 0.5);
            border: 1px solid #999;
            box-sizing: border-box;
            cursor: move;
            display: none;
        }
    </style>
</head>

<body>
    <div class="small">
        <img src="images/mouse.jpg" alt="">
        <div class="move"></div>
    </div>
    <div class="big">
        <img src="images/mouseBigSize.jpg" alt="">
    </div>

    <script src="index.js"></script>
</body>
<script>
    var small = document.querySelector(".small");
    var move = document.querySelector(".move");
    var bigDiv = document.querySelector(".big")
    var img = document.querySelector(".big>img")


    small.onmouseenter = function() {
        move.style.display = "block";
        bigDiv.style.display = "block"
        move.style.width = "200px";
        move.style.height = "200px";
    }
    small.onmouseleave = function() {
        move.style.display = "none";
        bigDiv.style.display = "none"
    }
    small.onmousemove = function(e) {
        //相对于容器的坐标

        //鼠标可移动距离 = 鼠标可视窗口 - 盒子距离body的left和top - 内盒子的宽度和高度
        var x = e.clientX - small.offsetLeft - move.offsetWidth / 2;
        var y = e.clientY - small.offsetTop - move.offsetHeight / 2;

        //如果鼠标在可视距离外,即e.clientX 或者 e.clientY为负数
        x = x <= 0 ? 0 : x;
        y = y <= 0 ? 0 : y;
        //如果鼠标可移动距离大于 大盒子宽度 - 小盒子移动宽度,则让他只能为最大宽度或者高度移动
        console.log(small.offsetWidth, move.offsetWidth)
        if (x >= (small.offsetWidth - move.offsetWidth)) {
            x = (small.offsetWidth - move.offsetWidth);
            console.log("x的最大宽度---------------------------------------------")
        }
        if (y >= (small.offsetHeight - move.offsetHeight)) {
            y = (small.offsetHeight - move.offsetHeight);
            console.log("x的最大高度---------------------------------------------")
        }
        // console.log("此时的移动宽度" + x, "此时的移动高度" + y)
        move.style.top = y + "px";
        move.style.left = x + "px";
        //img的展示的大小应该是黄色区域的倍数
        //所以倍数为盒子的大小除黄色区域的大小
        var numX = small.offsetWidth / move.offsetWidth;
        var numY = small.offsetHeight / move.offsetHeight;

        //img的展示的大小
        var doubleX = move.offsetWidth * numX
        var doubleY = move.offsetHeight * numY
        img.style.width = doubleX;
        img.style.height = doubleY;
        //黄色距离大框的x和y

        //img距离大框的边距*倍数
        img.style.top = -(y * numY) + "px";
        img.style.left = -(x * numX) + "px";

    }
</script>

</html>

如果大家有什么更好的建议,欢迎回访!

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
根据提供的引用内容,我没有找到uni-app自带的内容放大镜功能。但是,我们可以使用第三方插件来实现这个功能。下面是一个使用uni-image-viewer插件实现内容放大镜的例子: 1.首先,我们需要安装uni-image-viewer插件。在HBuilderX中,可以通过以下步骤安装: - 点击菜单栏的“工具”->“插件安装”,打开插件安装窗口。 - 在搜索框中输入“uni-image-viewer”,找到该插件并点击“安装”按钮进行安装。 2.在需要放大的图片所在的vue文件中,引入uni-image-viewer插件: ```javascript import uniImageViewer from '@/components/uni-image-viewer/uni-image-viewer.vue' ``` 3.在template中,使用uni-image-viewer组件来展示图片,并设置其mode属性为preview: ```html <uni-image-viewer :urls="imgUrls" :current="currentImg" mode="preview"></uni-image-viewer> ``` 4.在script中,定义imgUrls和currentImg变量,并在需要放大的图片上绑定点击事件,点击事件中设置currentImg的值并打开uni-image-viewer组件: ```javascript export default { components: { uniImageViewer }, data() { return { imgUrls: ['https://example.com/image1.jpg', 'https://example.com/image2.jpg'], currentImg: '' } }, methods: { openImageViewer(url) { this.currentImg = url this.$refs.imageViewer.open() } } } ``` 5.最后,在需要放大的图片上绑定点击事件,点击事件中调用openImageViewer方法: ```html <image src="https://example.com/image1.jpg" @click="openImageViewer('https://example.com/image1.jpg')"></image> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值