Image magnifier

Scenario

If you want to look the product carefully on Tmall or JingDong, you will move your mouse point(or tap on smartphone) over the images on the product detail page, and then a larger and clearer image will appear and just show the part of image where your mouse is pointing at in detail. This is the image magnifier on web, a smart way to show the detail part of the product. You may find it helpful when you are going to build up a website for shopping next time.

Implementation

<div class="small">
</div>
<div class="big">
</div>
<style>
    .small {
        float: left;
        height: 200px;
        width: 300px;
        background: url("a.jpg");
        background-size: cover;
    }

    .big {
        display: none;
        position: relative;
        height: 200px;
        width: 300px;
        background: url("a.jpg");
        background-size: 900px 600px;
        background-repeat: no-repeat;
    }
</style>
<script>
    function setup() {
        var small = document.querySelector(".small");
        var big = document.querySelector(".big");
        small.addEventListener("mousemove", function(event) {
            console.log(event);
            var x = event.offsetX * 3 - 150;
            var y = event.offsetY * 3 - 100;
            if (x < 0) {
                x = 0;
            } else if (x > 600) {
                x = 600;
            }
            if (y < 0) {
                y = 0;
            } else if (y > 400) {
                y = 400;
            }
            big.style.backgroundPosition = "-" + x + "px -" + y + "px";
            big.style.display = "block";
        });
        small.addEventListener("mouseout", function(event) {
            big.style.display = "none";
        });
    }
    setup();
</script>

Result

When you move your mouse over the image, you will see that another image showing up to exactly display the region with your mouse as the center, but at a larger scale. And when you move the mouse out, the detailed image will hide. It works as a handy magnifier, when you want to inspect, it show up and display the content larger, and when you move out it will disappear.

Explanation

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值