How to change an image path based on screen width?

前端 未结 6 1536
天涯浪人
天涯浪人 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:27

    You can use $(window).width(); to determine the size of the window, then set the src of the images accordingly:

    $(function() {
      if($(window).width() <= 310) {
        $("img").each(function() {
          $(this).attr("src", $(this).attr("src").replace("images/620x410/", "images/310x205/"));
        });
      }
    });
    

    You should also note that if I start my browser window at a size of <= 310px, then resize it to above that, your images will still be the smaller version. You might want to consider using the resize event to combat that if required: http://api.jquery.com/resize/

    0 讨论(0)
  • 2020-12-13 01:28

    I achieved a similar effect using Bootstrap Responsive utilities visible-xs and hidden-xs. I needed to use a different image set in a carousel depending on the size of the browser window.

            <div class="carousel slide hidden-xs" id="site-banner-big"  data-ride="carousel">   <!-- only show this carousel on large screens -->
      <!-- Wrapper for slides -->
      <div class="carousel-inner" role="listbox">
        <div class="item active"><center><img src="images/site-banner/large/image01.jpg" alt="Image"></center></div>
        <div class="item"><center><img src="images/site-banner/large/image02.jpg" alt="Image"></center></div>
        <div class="item"><center><img src="images/site-banner/large/image03.jpg" alt="Image"></center></div>
      </div><!-- end carousel-inner -->      
    </div><!-- end site-banner-big -->
    
    <div class="carousel slide visible-xs" id="site-banner-small"  data-ride="carousel">   <!-- only show this carousel on small screens -->
      <!-- Wrapper for slides -->
      <div class="carousel-inner" role="listbox">
        <div class="item active"><center><img src="images/site-banner/small/image01.jpg" alt="Image"></center></div>
        <div class="item"><center><img src="images/site-banner/small/image02.jpg" alt="Image"></center></div>
        <div class="item"><center><img src="images/site-banner/small/image03.jpg" alt="Image"></center></div>
      </div><!-- end carousel-inner -->      
    </div><!-- end site-banner-small -->
    
    0 讨论(0)
  • 2020-12-13 01:30

    I am trying to use the "image change path system" for different images inside the web page . The idea is instead of change the "background" image (so just a single image), i would like to change the images path depending on the screen orientation :

    as for php

    $("#imageId").attr("src", "your new url here");
    

    kind of this but dunno how

    @media screen and (orientation: landscape) 
    {
        .yourClass {
            content:url("images/landscape/");
        }
    }
    
    @media screen and (orientation: portrait) {
        .yourClass {
            content:url("images/portrait/");
        }
    }
    
    0 讨论(0)
  • 2020-12-13 01:34

    You Can use resize()

    $(window).resize(function() {
    if($(window).width() < 310 ) {
       $('img').attr('src','folder1'+filename);
    }
    if($(window).width() > 310 ) {
       $('img').attr('src','folder2'+filename);
    }
    

    });

    0 讨论(0)
  • 2020-12-13 01:46

    If CSS using media queries is an option for you, you could use a CSS only solution.

    Add the media queries to your CSS similar to the below:

    @media screen and (max-width: 310px) {
      .yourClass {
        content:url("images/310x205/image1.jpg");
      }
    }
    
    @media screen and (min-width: 620px) {
      .yourClass {
        content:url("images/620x410/image1.jpg");
      }
    }
    

    Add the myClass to your effected image elements similar to the below:

    <img class="yourClass">
    

    You might need to tweak the values a little for your needs.

    0 讨论(0)
  • 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.

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