【问题标题】:How to achieve image changing on hover?如何实现悬停时的图像变化?
【发布时间】:2014-11-22 03:01:38
【问题描述】:

所以我试图在这个website 上实现这样的效果。

靠近底部,您可以将鼠标悬停在图像上,当您移动时它会显示另一个

有什么想法吗?我的意思是我知道它们只是覆盖了两个图像,但是它们如何在悬停时使用 CSS/Javascript 显示远图像?这超出了我的范围。我试过自己复制它,但没有成功。

【问题讨论】:

标签: javascript jquery css image


【解决方案1】:

试试这个:

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>

Working Fiddle

【讨论】:

    【解决方案2】:

    所以发生的情况是,当鼠标悬停在线条上时,宽度会动态变化,如果您检查元素,您也可以看到这一点。

    现在,在 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);
    

    【讨论】:

    • 干得好!只有当它有人类可以理解的函数名称时才会很棒!
    【解决方案3】:

    如果你查看 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/
    祝我好运……

    【讨论】:

    • 这不是答案...这只是从该站点复制代码并将其粘贴到答案中...
    • 那答案是什么?答案是理论?你能提出什么理论?大爆炸理论?我知道我错了,因为我忘记了 js。
    • 任何人都可以复制和粘贴代码,他希望重新创建它。所以确定他是否想使用他可能拥有的所有代码,但我敢肯定他想了解发生了什么,而不是像没有头脑的僵尸一样复制和粘贴......
    • 大声笑......太搞笑了。 +1 只是为了好玩
    • @SimplyCraig 为什么要重新发明轮子?只因你想要?如果他想了解东西——为什么他不能看它的来源?限制他查看他们网站代码的许可证在哪里?
    【解决方案4】:
    $('img').on('mousemove', function(){
       var imgsrc = $(this).attr('src');
       if(imgsrc == 'img1.png'){
          $(this).attr('src','img2.png');
       }else{
          $(this).attr('src','img1.png');
       }
    });
    

    【讨论】:

    • 很确定他在谈论底部的汽车图像,该图像具有滑块效果以显示部分图像。不仅仅是直接替换悬停时的图像。
    • @SimplyCraig - 如果你不喜欢这个答案,你应该投反对票,而不是标记它。当答案甚至不是尝试回答时(即,如果他们留下评论、链接或其他问题,而不是尝试回答,作为答案,则应保留标志。)
    • @ArtOfWarfare 我从未标记过这篇文章,所以你在说什么?
    • @SimplyCraig - 抱歉,最近有人匿名举报了它,因为你最近发表了负面评论,我认为是你。
    • @ArtOfWarfare 哦,好吧,不是我。
    【解决方案5】:

    你可以在没有 javascript 的情况下做到这一点, JSfiddle - http://jsfiddle.net/k4915wwm/

    只是……

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

    【讨论】:

    • 我指的是滑块效果,而不是直接淡出。
    • 请仔细阅读问题并参考问题中提供的链接!!
    • 我想这个问题应该问得更好。我去了提供的链接,当你将鼠标悬停在它上面时,它只是有一些图像发生了变化。
    猜你喜欢
    • 2012-08-14
    • 2014-02-24
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多