-1

my image has data-web-src, data-tablet-src and data-mobil-src if my img tag has .lazy_res class instead of .lazyload then set value of data-web-src to src on web, but if screen (or device) is tablet than set data-tablet-src value's to src I want to do this for all images on my web page but I can’t find a solution here you are my code

$(document).ready(function() {

  function noLazyImages(e) {
    var getWebSrc = $(e).attr("data-web-src");
    var getTabletSrc = $(e).attr("data-tablet-src");
    var getMobilSrc = $(e).attr("data-mobil-src");
    if ($(".box img").hasClass("lazy_res")) {

      if ($(window).width() > 960) {
        $(e).attr("src", getWebSrc);
      } else if ($(window).width() < 768) {
        $(e).attr("src", getTabletSrc);
      }else if ($(window).width() < 480) {
        $(e).attr("src", getMobilSrc);
      }

    } else {
      // do nothing..
    }

  }

  noLazyImages(".box img");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box">

<img class="lazy_res" data-web-src="http://image.prntscr.com/image/bdf1d94b64104ef2acd2ceee19882cd1.jpg" data-mobil-src="http://image.prntscr.com/image/caa51ab4900448589201207e57b2630f.jpg" data-tablet-src="http://image.prntscr.com/image/4b2862a292b543139daa7805a58c17fd.jpg"/>
  
  
  
</div>
ani_css
  • 2,118
  • 3
  • 30
  • 73
  • 2
    Check https://api.jquery.com/data/ – Huangism Feb 09 '17 at 20:24
  • 1
    This might help http://stackoverflow.com/a/5309947/3076934 – amansoni211 Feb 09 '17 at 20:25
  • 1
    eh, there's nothing wrong with using .attr to access data-* attributes. It's even more performant than using jQuery's .data for them. – Kevin B Feb 09 '17 at 20:26
  • Hi I haven't any problem with data attribute I just have some problem with my jquery I'm keen on and I'm a beginner on jquery how can I do what I want ? – ani_css Feb 09 '17 at 20:28
  • @KevinB How is it faster to touch the DOM _every time_ than touching it once and using a JS object afterwards? – hashchange Feb 09 '17 at 20:31
  • @hashchange depends on how often and how many times. .data does quite a bit in the background to figure out what each data attribute contains (is it json, a number, a boolean, etc). In this case performance isn't even a concern. My main point in that comment was that pointing to the .data documentation or similar so questions is irrelevant to this question. – Kevin B Feb 09 '17 at 20:33

1 Answers1

2

you need to loop over all of them and get and set instance specific values

Can do it using attr(function)

 function noLazyImages(e) {
   $(e + '.lazy_res').attr('src', function(_, oldSrc) {
     var elData = $(this).data(),
         winWidth = $(window).width();
     if (winWidth  > 960) {
       return elData['webSrc']
     } else if (winWidth  < 768 && winWidth >=480) {
       return elData['tabletSrc']
     } else if (winWidth  < 480) {
       return elData['mobilSrc']
     }
   })
 }

DEMO

charlietfl
  • 170,828
  • 13
  • 121
  • 150