Simple JQuery script working on JSFiddle, not on my site

后端 未结 4 646
有刺的猬
有刺的猬 2021-01-20 03:02

I\'m trying to get the original width of an image with JQuery and make some adjustments in the CSS with a condition if an image is wider than 700px.

I used this code

4条回答
  •  逝去的感伤
    2021-01-20 03:07

    your variable $imgWidth is undeclared in your code, and imgWidth will not have scope outside your function.

    try:

    var img = new Image();
    img.src = $('#imageViewerImg').attr('src');
    var imgWidth='';
    img.onload = function() {
        imgWidth = this.width;
    }
    if(imgWidth > 700) {
        $("#photoHolder").css({"vertical-align":"none","text-align:":"none"});
    }
    

提交回复
热议问题