第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

商城放大鏡效果js實(shí)例

標(biāo)簽:
Html5 JavaScript CSS3

首先为大家展示一下实例的最终效果

https://img1.sycdn.imooc.com//5c47d75e00012cdb00400016.jpg

首先准备静态的样式

https://img1.sycdn.imooc.com//5c403dbd00018a0a14170798.jpg

编写HTML结构

https://img1.sycdn.imooc.com//5c4038490001870f12190349.jpg

<div class="bigBox">
    <div class="commodityBox">
        <div class="commodity"><img class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="images/12.jpg"><span></span></div>
        <ul class="commodityM">
            <li><img class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="images/12.jpg"></li>
            <li><img class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="images/13.jpg"></li>
            <li><img class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="images/14.jpg"></li>
            <li><img class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="images/15.jpg"></li>
        </ul>
        <div class="commodityBig"><img class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="images/12.jpg"></div>
    </div>
</div>

css样式


<style>
*{
    margin: 0;
    padding: 0;
}
ol,ul,li{
    list-style: none;
}
.bigBox{
    margin: 50px;
}
img{
    width: 100%;
    height: 100%;
}
.commodityBox{
    position: relative;
}
.commodity{
    position: relative;
    width: 350px;
    height: 280px;
    margin-bottom: 6px;
    border: 1px solid gray;
}
.commodity>span{
    display: none;
    position: absolute;
    top:0;
    left: 0;
    width: 80px;
    height: 80px;
    background-color: rgba(250,250,0,.4);
    cursor:move;
}
.commodityM{
    display: flex;
    flex-direction:row;
    justify-content:space-between;
    width: 350px;
    height: 100px;
}
.commodityM>li{
    width: 24%;
}
.commodityBig{
    display: none;
    position: absolute;
    top:0;
    left: 360px;
    width: 700px;
    height: 560px;
    border: 1px solid gray;
    overflow: hidden;
}
.commodityBig>img{
    position: absolute;
    top: 0;
    left: 0;
    transform:scale(1.1);
}
</style>

https://img1.sycdn.imooc.com//5c403f5400014d7711850440.jpg


https://img1.sycdn.imooc.com//5c409ec30001231e11150683.jpg

https://img1.sycdn.imooc.com//5c40a3400001bc3b06670640.jpg

<script>
    window.onload = function(){

        var commodity  = document.querySelector(".commodity");
        var commoditySpen = document.querySelector(".commodity>span");
        var commodityM = document.querySelector(".commodityM");
        var commodityBig = document.querySelector(".commodityBig");
        var commodityImg =  document.querySelector(".commodity>img");
        var commodityBigImg =  document.querySelector(".commodityBig>img");
        var commodityMson = commodityM.children;

        commodity.onmouseover = function(){
            commoditySpen.style.display = 'block';
            commodityBig.style.display = 'block';
            commodity.onmousemove = function(event){
                var pointX = event.clientX - commodity.offsetParent.offsetLeft - commoditySpen.offsetWidth*0.5;
                var pointY = event.clientY - commodity.offsetParent.offsetTop - commoditySpen.offsetHeight*0.5;
                if(pointX < 0){
                    pointX = 0;
                }else  if(pointX >= commodity.offsetWidth - commoditySpen.offsetWidth){
                    pointX = commodity.offsetWidth - commoditySpen.offsetWidth;
                }
                if(pointY < 0){
                    pointY = 0;
                }else  if(pointY >= commodity.offsetHeight - commoditySpen.offsetHeight){
                    pointY = commodity.offsetHeight - commoditySpen.offsetHeight;
                }
                commoditySpen.style.left = pointX + "px";
                commoditySpen.style.top = pointY + "px";

                commodityBigImg.style.left = - pointX / (2*commodity.offsetWidth / commodityBig.offsetWidth)+"px";
                commodityBigImg.style.top =  - pointY / (2*commodity.offsetHeight / commodityBig.offsetHeight)+"px";

            }
        };
        commodity.onmouseout = function(){
            commoditySpen.style.display = 'none';
            commodityBig.style.display = 'none';
        };
        console.log(commodityM.children);

        for(var i=0;i<commodityM.children.length;i++){
            (function(){
                var commodityLi = commodityM.children[i];
                console.log(commodityM.children[i]);
                commodityM.children[i].onmouseover = function(){
                    var img = commodityLi.lastChild;
                    commodityImg.src = img.src;
                    commodityBigImg.src = img.src
                    console.log(img.src);
                };
            })(i)
        }
    }
</script>

https://img1.sycdn.imooc.com//5c47d8fa0001901911440812.jpg

网上有许多不同的插件和框架可以实现不同的效果(轮播图、选项卡、放大镜等等),工作上为了简便高效,用插件确实是不错的做法,但是在学习的过程中我觉得更应该通过原生的js去将这些常见的效果实现一遍,理解其中的原理,这样更有利于自己的进步和让自己不对插件框架等,产生过大的依赖性。

點(diǎn)擊查看更多內(nèi)容
1人點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶(hù)
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專(zhuān)欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消