放大镜原理

在浏览各大网购网站时(淘宝、京东等),图片放大效果是常见的一个功能。

放大镜

实现思路

  1. 鼠标移入小图片时,放大的图片区域会显示出来,同时小图片上有一个提示被放大的区域;
  2. 当移出小图片时,放大的图片隐藏,提示区域也被隐藏;
  3. 在移动过程中提示框也会跟着鼠标进行移动。

相关技术

  1. 鼠标事件;
  2. 背景图片样式;
  3. 元素的尺寸属性和数学计算;

HTML 骨架

<div id="main">
    <div class="wrapper">
        <!-- 小图和提示被放大的区域 -->
        <div class="area"></div>
        <img src='./01.jpg' />
    </div>
    <!-- 大图展示区域 -->
    <!-- 使用背景图片来展示,而不是 img 标签 -->
    <div class="showBox"></div>
</div>

CSS 样式

#main{
    display: flex;
    flex-direction: row;
    height: 300px;
    align-items: center;
}
#main .wrapper img{
    height: 300px;
    /* 不设置宽度了,让宽度自适应 */
}
div.wrapper{
    position: relative;
    height: 300px;
    cursor: move;
}
div.area{
    /* 这里使用定位,让滑块能运动 */
    position: absolute;
    display: none;
    background-color: rgba(255, 255, 255, 0.3);
}
div.showBox{
    /* 让放大图片的元素宽高与小图片宽高一样 */
    height: 300px;
    margin-left: 20px;
    display: none;
    background-repeat: no-repeat;
}

上面代码中,图片没有设置高度,我们在JavaScript中用 img.offsetWidth 来获取。获取之后设置 wrapper 的宽度与 img 的宽度相同。

JavaScript 的编写

首先先捋一下思路。
因为要动态获取图片宽度,然后初始化一些样式。

window.onload = function(){
    const img = document.querySelector('img');
    const wrapper = document.querySelector('.wrapper');

    const slide = document.querySelector('.area');
    const bigView = document.querySelector('.showBox');

    const img_src = img.getAttribute('src');
    const img_W = img.offsetWidth;
    const img_H = img.offsetHeight;

    wrapper.style.width = img_W + 'px';
    bigView.style.width = img_W + 'px';
    // 设置 bigView 的图片路径
    bigView.style.backgroundImage = `url(${img_src})`;
}

关于图片放大几倍,写一个 init 方法,这个方法传入一个参数,这个参数是个数字类型的参数,表示图片的放大倍数。

function init(n){
    // ....
}

这样就可以动态设置滑块的大小,以及放到图片的大小。

function init(n){
    // 缩小几倍,slide 的宽高相应的也要缩小几倍
    slide.style.width = img_W / n + 'px';
    slide.style.height = img_H / n + 'px';
    // 放大 n 倍,图的宽高要乘以 n
    bigView.style.backgroundSize = `${img_W * n}px ${img_H * n}px`;
}

bigView 的样式相当于这样的:

在这里插入图片描述
下面就是鼠标在 wrapper 上移动时,滑块跟着移动,然后大图的背景区域也做变化。

wrapper.onmousemove = function (e) {
    // 移入后,滑块和大图都展示出来
    bigView.style.display = 'block';
    slide.style.display = 'block';

    // 注意这里,这里是让鼠标坐标减去滑块大小的一半
    // 这样做可以让鼠标的位置在滑块的中心处
    var leftX = e.clientX - slide.offsetWidth / 2,
        topY = e.clientY - slide.offsetHeight / 2;

    // 下面的判断是为了让鼠标移动时滑块更够在指定的区域显示
    if (leftX < 0) {
        leftX = 0;
    } else if (leftX > (img_W - slide.offsetWidth)) {
        // 这里是不让滑块宽度超过展示区域
        leftX = img_W - slide.offsetWidth;
    }

    if (topY < 0) {
        topY = 0;
    } else if (topY > (img_H - slide.offsetHeight)) {
        // 这里是不让滑块高度超过展示区域
        topY = img_H - slide.offsetHeight;
    }

    // 然后让优化后的值赋给滑块的 left 和 top
    slide.style.left = leftX + 'px';
    slide.style.top = topY + 'px';

    // 这里变换图片的位置,展示应该被展示的区域
    bigView.style.backgroundPosition = `${leftX * (-n)}px ${topY * (-n)}px`;
}

通过判断,可以让滑块滑动的区域限制在 wrapper 区域中,而且鼠标基本都在滑块的中心位置。

最后移出鼠标,滑块和大图消失。

wrapper.onmouseout = function () {
    bigView.style.display = 'none';
    slide.style.display = 'none';
}

最终效果图

最终效果

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
望远镜的原理可以用Python来实现。根据引用\[1\]和引用\[2\]的描述,望远镜由物镜和目镜组成。物镜的作用是使远处的物体在焦点附近成实像,目镜的作用是放大这个实像。下面是一个简单的Python代码示例,用于实现望远镜的原理: ```python import matplotlib.pyplot as plt def telescope(f_objective, f_eyepiece, d): # 物镜焦距 f1 = f_objective # 目镜焦距 f2 = f_eyepiece # 物镜与目镜之间的距离 distance = d # 物镜成像 image1 = f1 / (f1 - distance) # 目镜放大 image2 = -f2 / (f2 - image1) # 绘制光路图 plt.plot(\[0, -distance, -distance - image1, -distance - image1 - image2\], \[0, 0, 0, 0\], 'ro-') plt.plot(\[-distance, -distance\], \[-0.5, 0.5\], 'k--') plt.plot(\[-distance - image1, -distance - image1\], \[-0.5, 0.5\], 'k--') plt.plot(\[-distance - image1 - image2, -distance - image1 - image2\], \[-0.5, 0.5\], 'k--') plt.text(-distance / 2, -0.7, '物镜') plt.text(-distance - image1 / 2, -0.7, '实像') plt.text(-distance - image1 - image2 / 2, -0.7, '放大像') plt.xlabel('光路') plt.ylabel('光线高度') plt.title('望远镜光路图') plt.ylim(-1, 1) plt.show() # 设置物镜焦距、目镜焦距和物镜与目镜之间的距离 f_objective = 10 f_eyepiece = 5 d = 15 # 调用望远镜函数 telescope(f_objective, f_eyepiece, d) ``` 这段代码使用matplotlib库绘制了望远镜的光路图。你可以根据需要调整物镜焦距、目镜焦距和物镜与目镜之间的距离来观察不同的效果。 #### 引用[.reference_title] - *1* *2* *3* [使用Turtle库绘制望远镜和显微镜成像系统原理图](https://blog.csdn.net/weixin_43810267/article/details/111595074)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值