使用if的Html属性控件

时间:2017-02-25 10:51:32

标签: javascript jquery html css

我的img有3个属性但是如果有一个属性(data-web-srcdata-tablet-srcdata-mobil-src)比设置我的src而不是其中一个属性可能吗?

please click to see on codepen



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

$(document).ready(function() {
  noLazyImages("body img");
})
&#13;
img {
  width: 300px;
}
&#13;
<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"
/>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

我认为以下代码可以满足您的需求。

我从问题代码段中的x <- data.frame(y=c(1, 1, 1, 0, 0, 0), k1=c(4,3,5,1,2,3), k2= c(6,7,8,5,6,3)) logistic_regression <- function(x){ glm.out <- glm(as.formula(paste("y~", paste(colnames(x[,-1]), collapse="+"))), family=binomial(logit), data=x) return(summary(glm.out)) } logistic_regression(x) 标记中删除了 private static double distanceRadians(double lat1, double lng1, double lat2, double lng2) { return arcHav(havDistance(lat1, lat2, lng1 - lng2)); } /** * Returns the angle between two LatLngs, in radians. This is the same as * the distance on the unit sphere. */ static double computeAngleBetween(LatLng from, LatLng to) { return distanceRadians(toRadians(from.latitude), toRadians(from.longitude), toRadians(to.latitude), toRadians(to.longitude)); } /** * Returns the distance between two LatLngs, in meters. */ public static double computeDistanceBetween(LatLng from, LatLng to) { return computeAngleBetween(from, to) * EARTH_RADIUS; } /** * Computes inverse haversine. Has good numerical stability around 0. * arcHav(x) == acos(1 - 2 * x) == 2 * asin(sqrt(x)). * The argument must be in [0, 1], and the result is positive. */ static double arcHav(double x) { return 2 * asin(sqrt(x)); } /** * Returns haversine(angle-in-radians). * hav(x) == (1 - cos(x)) / 2 == sin(x / 2)^2. */ static double hav(double x) { double sinHalf = sin(x * 0.5); return sinHalf * sinHalf; } /** * Returns hav() of distance from (lat1, lng1) to (lat2, lng2) on the unit sphere. */ static double havDistance(double lat1, double lat2, double dLng) { return hav(lat1 - lat2) + hav(dLng) * cos(lat1) * cos(lat2); } ,以便您可以同时运行并比较结果。

<强>逻辑

如果data-tablet-src不存在,

img将为false。因此它将返回if(elData['tabletSrc'])

elData['tabletSrc']
return elData['webSrc']

答案 1 :(得分:1)

您的脚本工作正常,您只需要向窗口调整大小事件添加侦听器:

$(document).ready(function() {
  noLazyImages("body img"); // on Document ready

  $( window ).resize(function() {
    noLazyImages("body img"); // on window Resize
  });

})