簡體   English   中英

如何在懸停時實現圖像變換?

[英]How to achieve image changing on hover?

所以我試圖在這個網站上實現這樣的效果。

在您可以將鼠標懸停在圖像上方的底部附近,當您在圖像上移動時它會顯示另一個圖像

有任何想法嗎? 我的意思是我知道他們只是覆蓋了這兩個圖像,但他們如何在懸停時使用CSS / Javascript顯示遠端圖像? 這超出了我的范圍。 我試過自己復制它沒有成功。

嘗試這個:

 var main = document.querySelector('.main'); var one = document.querySelector('.one'); var two = document.querySelector('.two'); main.onmousemove = function (e) { var width = e.pageX - e.currentTarget.offsetLeft; one.style.width = width + "px"; two.style.left = width + "px"; } 
 .main { width: 200px; height: 200px; overflow: hidden; position: relative; } .one { background-image: url('http://www.lorempixel.com/200/200'); width: 50%; height: 100%; } .two { background-image: url('http://www.lorempixel.com/200/200/sports'); position: absolute; left: 50%; right: 0px; top: 0px; bottom: 0px; background-position: right; } 
 <div class="main"> <div class="one"></div> <div class="two"></div> </div> 

工作小提琴

所以發生的事情是,當鼠標懸停在線上時,寬度會動態變化,如果你檢查元素,你也可以看到它。

現在,在DOM結構中,棕色汽車,被隱藏的汽車在頂部圖像之前。 所以要實現這一點,你可以絕對定位棕色汽車,所以它就在下一個圖像后面,並且使用javascript或jQuery添加一個用於懸停的監聽器,在圖像或網站上使用的中間線上,這將改變頂部圖像(銀色圖像)相對於圖像塊中鼠標位置的寬度。

基本上,背景圖像的寬度應該按百分比改變鼠標距離DIV左邊的距離百分比,即如果鼠標位於中間,則寬度應為50%。

這是他們用來做它的js,就在他們的網站上(我沒壓縮它)

var ImageSlider = ImageSlider || {};
ImageSlider.Public = function(t) {
"use strict";
var e = t(".image-compare-tool"),
    i = t(".image-compare-images"),
    o = t(".image-compare-top img"),
    a = (t(".image-compare-bottom img"), function(e) {
        e.preventDefault();
        var i, o = t(this).find(".image-compare-top"),
            a = t(this).find(".image-compare-bottom img")[0],
            n = a.getBoundingClientRect();
        i = "mousemove" == e.originalEvent.type ? (e.pageX - n.left) / a.offsetWidth * 100 : (e.originalEvent.touches[0].pageX - n.left) / a.offsetWidth * 100, 100 >= i && o.css({
            width: i + "%"
        })
    }),
    n = function() {
        i.each(function() {
            t(this).on("mousemove", a), t(this).on("touchstart", a), t(this).on("touchmove", a)
        })
    },
    m = function() {
        o.each(function() {
            var e = t(this).attr("src"),
                i = t(this).parent();
            i.css("background-image", "url(" + e + ")")
        })
    },
    c = function() {
        n(), m()
    },
    r = function() {
        e.length > 0 && c()
    };
r()}(jQuery);

如果您查看html源代碼(Chrome中的Ctr + Shift + I),您可以看到此元素

<div class="image-compare-tool ICT-theverge">
  <div class="image-compare-images">
    <div class="image-compare-bottom"><img src="http://cdn2.vox-cdn.com/uploads/chorus_asset/file/2455624/khyzyl-saleem-plain-copylow.0.jpg"></div>
    <div class="image-compare-top" style="width: 6.158357771261%; background-image: url(http://cdn0.vox-cdn.com/uploads/chorus_asset/file/2455620/khyzyl-saleem-plain-copylow1.0.jpeg);"><img src="http://cdn0.vox-cdn.com/uploads/chorus_asset/file/2455620/khyzyl-saleem-plain-copylow1.0.jpeg"></div>
  </div>
</div>

所以這是圖像! 接下來你需要看一下css。

.image-compare-tool
{
    max-width:100%;
    width:100%;
    z-index:999;
    margin:0 auto 1.5em 0;
}

.image-compare-images
{
    font-size:0;
    position:relative;
    height:100%;
    -ms-touch-action:none;
    -webkit-touch-callout:none;
    -webkit-user-select:none;
}

.image-compare-images:hover
{
    cursor:col-resize;
}

.image-compare-images img
{
    display:block;
    height:auto;
    width:100%;
}

.image-compare-top,.image-compare-bottom
{
    z-index:0;
    height:100%;
}

.image-compare-top
{
    background-size:cover;
    height:100%;
    left:0;
    position:absolute;
    top:0;
    width:50%;
}

.image-compare-top:after
{
    background-color:#fff;
    content:'';
    height:50px;
    left:calc(100%-5px);
    top:calc(50%-25px);
    position:absolute;
    width:10px;
}

.image-compare-top img
{
    display:none;
}

.ICT-SBNation .image-compare-top:after
{
    background-color:#c52126;
}

.ICT-SBNation .image-compare-top:before
{
    background-color:#c52126;
    content:'';
    height:100%;
    left:calc(100%-2.5px);
    top:0;
    position:absolute;
    width:5px;
}

.ICT-TheVerge .image-compare-top:after
{
    background-color:#fa4b2a;
}

.ICT-TheVerge .image-compare-top:before
{
    background-color:#fa4b2a;
    content:'';
    height:100%;
    left:calc(100%-2.5px);
    top:0;
    position:absolute;
    width:5px;
}

.ICT-Polygon .image-compare-top:after
{
    background-color:#ff0052;
}

.ICT-Polygon .image-compare-top:before
{
    background-color:#ff0052;
    content:'';
    height:100%;
    left:calc(100%-2.5px);
    top:0;
    position:absolute;
    width:5px;
}

.image-compare-top:before,.ICT-Vox .image-compare-top:before
{
    background-color:#fff;
    content:'';
    height:100%;
    left:calc(100%-2.5px);
    top:0;
    position:absolute;
    width:5px;
}

在這里我們! 您可以通過僅包含css並使用僅更改img路徑制作相同的html結構和類來實現相同的內容...

最后我從上面的答案中肆無忌憚地偷走了js:

var ImageSlider = ImageSlider || {};
ImageSlider.Public = function (t) {
    "use strict";
    var e = t(".image-compare-tool"),
        i = t(".image-compare-images"),
        o = t(".image-compare-top img"),
        a = (t(".image-compare-bottom img"), function (e) {
            e.preventDefault();
            var i, o = t(this).find(".image-compare-top"),
                a = t(this).find(".image-compare-bottom img")[0],
                n = a.getBoundingClientRect();
            i = "mousemove" == e.originalEvent.type ? (e.pageX - n.left) / a.offsetWidth * 100 : (e.originalEvent.touches[0].pageX - n.left) / a.offsetWidth * 100, 100 >= i && o.css({
                width: i + "%"
            })
        }),
        n = function () {
            i.each(function () {
                t(this).on("mousemove", a), t(this).on("touchstart", a), t(this).on("touchmove", a)
            })
        },
        m = function () {
            o.each(function () {
                var e = t(this).attr("src"),
                    i = t(this).parent();
                i.css("background-image", "url(" + e + ")")
            })
        },
        c = function () {
            n(), m()
        },
        r = function () {
            e.length > 0 && c()
        };
    r()
}(jQuery);

工作的JSFiddle: http//jsfiddle.net/9gf59k00/
祝我好運......

$('img').on('mousemove', function(){
   var imgsrc = $(this).attr('src');
   if(imgsrc == 'img1.png'){
      $(this).attr('src','img2.png');
   }else{
      $(this).attr('src','img1.png');
   }
});

沒有javascript就可以做到這一點,JSfiddle - http://jsfiddle.net/k4915wwm/

只是…

div:hover {
   background:url("newImage.jpg");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM