How to change an image path based on screen width?

前端 未结 6 1540
天涯浪人
天涯浪人 2020-12-13 01:06

I\'m trying to change the folder that my images are read from depending on the window width.

I have two folders that contain the different image sizes and I\'m wonde

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-13 01:46

    I don't think waiting for DOM Ready (jQuery's $(document).ready)) is a good idea in this case. Better use plain javascript:

    if (screen.width < 620) {
        var imgs = document.getElementsByTagName("img");
        for (var i = 0; i < imgs.length; i++) 
        {
            imgs[i].src = imgs[i].src.replace("images/620x410/", "images/310x205/");
        }
    }
    

    Paste it at the bottom of your body tag.

提交回复
热议问题