cross-fade images in jQuery

前端 未结 1 1442
情歌与酒
情歌与酒 2021-01-26 02:45

I have an image on a web page, and have it change when the jQuery slider is moved:

HTML:

\"Isa

        
相关标签:
1条回答
  • 2021-01-26 03:30

    here you go, quiet simple I suppose, much less code and difficulty comparing with implementing some plugins: http://jsfiddle.net/gcvqr/2/

    HTML

    <img src="http://macnetized.com/wp-content/uploads/2014/02/Apple_Logo_csh_by_wiimon.png" id="slider">
    <button>Change Image</button>
    

    jQuery

    var stopWorking=false;
    var firstSrc="http://macnetized.com/wp-content/uploads/2014/02/Apple_Logo_csh_by_wiimon.png";
    var secondSrc="http://technicallyeasy.net/wp-content/uploads/2011/04/apple-logo.jpg";
    $('button').click(function(){
        if(!stopWorking){
            stopWorking=true;
        $('body').append('<img src="'+$('#slider').attr('src')+'" id="fadingImg">');
        $('#fadingImg').css({
            'position':'absolute',
            'width':'400px',
            'height':'400px',
            'top':'0px',
            'left':'0px'
        });
            if($('#slider').attr('src')==firstSrc){
                $('#slider').attr('src',secondSrc);
            } 
            else{
                $('#slider').attr('src',firstSrc);
            }
        $('#fadingImg').fadeOut(1000,function(){
            $('#fadingImg').remove();
            stopWorking=false;
        });
        }
    });
    

    NOTE:

    in order to make it work in your page, you need to give #fadingImg the style of your slider img, to be positioned right over it.

    0 讨论(0)
提交回复
热议问题