Using jQuery to change image src when browser is resized

前端 未结 4 1013
慢半拍i
慢半拍i 2021-01-14 01:45

I have two different sized images, one if for screens smaller than 759px, the other is for screens larger than 759px.

I have managed to get the source of the images

4条回答
  •  长情又很酷
    2021-01-14 02:43

    Your code works fine, as I can see, but I prefer to use setTimeout, some times page could slow down without resize pause.

    $(document).ready(function() {
        var resizeTimer,
            $window = $(window);
    
        function imageresize()
        {
            if ($window.width() < 700)
            {
                $('.fluidimage').text('< 700');
            }
            else
            {
                $('.fluidimage').text('>= 700');
            }
        }
        imageresize();//Triggers when document first loads
    
        $window.resize(function() {
            clearTimeout(resizeTimer);
            resizeTimer = setTimeout(imageresize, 100);
        });
     }); 
    

    Example: jsfiddle.net/SAbsG/

提交回复
热议问题