简体   繁体   English

在使用 javascript 更改 img src 时添加过渡

[英]Add transition while changing img src with javascript

I have an img tag that I want to change the src when hover and it all works but i would like to add some transition so it doesn't look so rough but since it's an img src i cant target it with css.我有一个 img 标签,我想在悬停时更改 src,一切正常,但我想添加一些过渡,所以它看起来不那么粗糙,但由于它是一个 img src,我不能用 css 定位它。

http://jsfiddle.net/Ne5zw/1/ http://jsfiddle.net/Ne5zw/1/

html html

<img id="bg" src="img/img1.jpg">
<div onmouseover="imgChange('img/img2.jpg'); "onmouseout="imgChange('img/img1.jpg');">

js js

function imgChange(im){
document.getElementById('bg').src=(im);
}

You want a crossfade.你想要一个淡入淡出。 Basically you need to position both images on top of each other, and set one's opacity to 0 so that it will be hidden:基本上,您需要将两个图像放在彼此的顶部,并将其opacity设置为 0,以便将其隐藏:

<div id="container">
    <img class="hidden image1" src="http://www.istockphoto.com/file_thumbview_approve/4629609/2/istockphoto_4629609-green-field.jpg">

    <img class="image2" src="http://www.istockphoto.com/file_thumbview_approve/9958532/2/istockphoto_9958532-sun-and-clouds.jpg" />
</div>

CSS: CSS:

.hidden{
 opacity:0;   
}

img{
    position:absolute;
    opacity:1;
    transition:opacity 0.5s linear;
}

With a transition set for opacity on the images, all we need to do is trigger it with this script:在图像上设置了opacitytransition ,我们需要做的就是使用以下脚本触发它:

$(function(){
    debugger;
    $(document).on('mouseenter', '#hoverMe', function(){
        $('img').toggleClass('hidden');
    });
});

http://jsfiddle.net/Ne5zw/12/ http://jsfiddle.net/Ne5zw/12/

Here is a pure css solution using css transition .这是使用 css transition的纯 css 解决方案。 You can use a div as the container and set the background-image on hover.您可以使用div作为容器并在悬停时设置background-image

 .image-container { background: url(http://placeholder.pics/svg/300x300/DEDEDE/555555/Old%20Image) center center no-repeat; background-size: contain; width: 150px; height: 150px; -webkit-transition: all .3s ease-in-out; -moz-transition: all .3s ease-in-out; transition: all .3s ease-in-out; } .image-container:hover { background-image: url("http://placeholder.pics/svg/300x300/DEDEDE/555555/New%20Image"); }
 <div class="image-container"></div>

Just in case someone is curious how to actually create a transition-like effect when you are actually changing the source attribute of an image, this was the solution I came up with.以防万一有人好奇,当您实际更改图像的源属性时,如何实际创建类似过渡的效果,这就是我想出的解决方案。

Javascript: Javascript:

        var bool = false;
        setInterval(() => {
            bool = !bool;
            let imgSrc = bool ? 'hero-bg2.jpg' : 'hero-bg.jpg'; // Toggle image
            $('.parallax-slider').addClass('transitioning-src'); // Add class to begin transition
            setTimeout(() => {
                $('.parallax-slider').attr('src', `https://website.com/images/${imgSrc}`).removeClass('transitioning-src');
            }, 400); // Ensure timeout matches transition time, remove transition class
        }, 6000);

CSS: CSS:

.parallax-slider {
    transition: opacity 0.4s ease-in;
    -webkit-transition: opacity 0.4s ease-in;
    -moz-transition: opacity 0.4s ease-in;
    -ms-transition: opacity 0.4s ease-in;
    -o-transition: opacity 0.4s ease-in;
    opacity: 1;
}
.transitioning-src {
    transition: opacity 0.4s ease-out;
    -webkit-transition: opacity 0.4s ease-out;
    -moz-transition: opacity 0.4s ease-out;
    -ms-transition: opacity 0.4s ease-out;
    -o-transition: opacity 0.4s ease-out;
    opacity: 0;
}

This will give the illusion of 'fading to black and back' between images - even if you're using something like parallax.js where you have a data-attribute driven component that renders out into a dynamic image.这将在图像之间产生“变黑并变回”的错觉——即使您使用的是 parallax.js 之类的东西,其中您有一个数据属性驱动的组件,可以渲染成动态图像。 Hope it helps someone.希望它可以帮助某人。

Fixed Mister Epic solution's images in this jsfiddle .修复了此jsfiddleMister Epic 解决方案的图像。

HTML HTML

<div id="container">
  <img class="hidden image1" src="http://placeholder.pics/svg/300x300/DEDEDE/555555/Old%20Image">
  <img class="image2" src="http://placeholder.pics/svg/300x300/DEDEDE/555555/New%20Image" />
</div>
 <div id="hoverMe">hover me</div>

CSS CSS

    div#hoverMe {
    background-color:yellow;
    width:50px;
    height:50px;
    position:fixed;
    top:300px;
}

div#container{
   position:relative;
    height:200px;
}

.hidden{
 opacity:0;   
}

img{
    position:absolute;
    opacity:1;
    transition:opacity 0.5s linear;
}

JS JS

$(function(){
    $(document).on('mouseenter', '#hoverMe', function(){
        $('img').toggleClass('hidden');
    });
});

i usually use jquery for this. 我通常使用jquery。

$(document).on("mouseover", '.image_class', function () {
        $(this).attr('src', 'img/img2.jpg');
    });

$(document).on("mouseout", '.image_class', function () {
        $(this).attr('src', 'img/img1.jpg');
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM