2014-12-02 85 views
7

我正在嘗試確保圖像較大,然後在我的圖像查看器中打開它們的查看寬度。然而,在720p分辨率和谷歌瀏覽器中,原始寬度和視口寬度都爲0 ...Chrome未檢測到圖像大小

下面是一個示例頁面,其中應該在BFX視圖中打開圖像(在Chrome中嘗試較低的分辨率):Live Example

登錄(掃描第一圖像是所討論的圖像)

Starting BFX View Version 0.3 Build 61 alpha 
bfxcore.js:92 BFX View -> Looking for images in: .postpreview... 
bfxcore.js:92 BFX View -> Looking for images in: .content... 
bfxcore.js:109 Image: http://media-cache-ec0.pinimg.com/originals/ed/e1/c7/ede1c78fe16fba4afd1606772a5fc1ac.jpg Original Width: 0 Against: 0 
bfxcore.js:109 Image: images/smilies/wink.png Original Width: 0 Against: 0 
bfxcore.js:109 Image: images/smilies/smile.png Original Width: 0 Against: 0 
bfxcore.js:109 Image: images/primus/blue/misc/quote_icon.png Original Width: 0 Against: 0 
bfxcore.js:109 Image: images/primus/blue/buttons/viewpost-right.png Original Width: 0 Against: 0 
bfxcore.js:109 Image: images/smilies/wink.png Original Width: 0 Against: 0 
bfxcore.js:109 Image: images/smilies/smile.png Original Width: 0 Against: 0 
bfxcore.js:109 Image: images/primus/blue/misc/quote_icon.png Original Width: 0 Against: 0 
bfxcore.js:109 Image: images/primus/blue/buttons/viewpost-right.png Original Width: 0 Against: 0 

的JavaScript

$(function(){ 

    /**************************************************** 
    / BFX View version 0.3 build 56 
    / by WASasquatch for BeeskneesFX.com 
    /***************************************************/ 

    // Global vars 
    var appname = 'BFX View', 
     appflag = 'alpha', 
     appversion = 0.3, 
     appbuild = 61, 
    // Selectors 
     findImagesIn = new Array(
        '.postpreview', 
        '.content', 
        '.restore', 
        '.postbody' 
        ), // Master container class/id - all image tags in children elements get selected 
    // Theater selectors 
     theater = $('#theater-box'), 
     theaterimg = theater.find('#theater-img'), 
     theaterclose = theater.find('#theater-header span');   

    console.log('Starting '+appname+' Version '+appversion+' Build '+appbuild+' '+appflag); 
    if (notMobile === false) { 
     console.log(appname+' detected a mobile device. Disabling BFX View for performance. Visit us on a desktop!'); 
    } else { 
     // Start a BFX View selector 
     for (i=0; i<findImagesIn.length; i++) { 
      console.log(appname+' -> Looking for images in: '+findImagesIn[i]+'...'); 
      $(findImagesIn[i]).each(function(){ 
       bfxView('.'+$(this).attr('class')); 
      }); 
     } 
    } 

    function bfxView(id) { 
     var imgCount = 0; 
     $(id).each(function(){ 
      $(this).find('img').each(function() { 
       var img = $(this), 
        width, height, origWidth = $(this).width(); 
        hiddenImg = img.clone().attr('class','').attr('id','').css('visibility', 'hidden').removeAttr('height').removeAttr('width').appendTo('#loading-images'); 
       height = hiddenImg.height(); 
       width = hiddenImg.width(); 
       hiddenImg.remove(); 
       console.log('Image: '+$(this).attr('src')+' Original Width: '+origWidth+' Against: '+width); 
       if (width > origWidth) { 
        imgCount++; 
        $(this).css('cursor', 'pointer'); 
        var parent = $(this).parent(); 
        if (parent.attr('href') == $(this).attr('src')) { 
         parent.attr('target','_self'); 
         parent.removeAttr('href'); 
        } 
        $(this).click(function() { 
         var startingPoint = $(document).scrollTop(), 
          theaterActive = true, 
          bodyo = $('body').css('overflow'); 
          $('body').css('overflow', 'hidden'); 
         theaterimg.html('<img src="' + $(this).attr('src') + '" alt="Medium View" />'); 
         setTimeout(function(){ 
          theaterimg.find('img').each(function(){ 
           var renderWidth = $(this).width(); 
           if (renderWidth < width) { 
            $(this).css('cursor', 'pointer'); 
            theater.find('#viewfull').attr('href', '/viewer.html?src='+Base64.encode($(this).attr('src'))+'&t='+Base64.encode(window.location.href)); 
            theater.on('click', '#theater-img img', function(){ 
             window.location.href = '/viewer.html?src='+Base64.encode($(this).attr('src'))+'&t='+Base64.encode(window.location.href); 
            }); 
           } else { 
            theater.find('#viewfull').remove(); 
            $(this).attr('alt','Full Resolution View'); 
           } 
          }); 
         },0); 
         theater.fadeIn(1000, function() { 
          theaterclose.click(function() { 
           theater.fadeOut(1000, function() { 
            theaterActive = false; 
           }); 
           $('body').css('overflow', bodyo); 
          }); 
         }); 

        }); 

       } 

      }); 

     }); 

     console.log(appname+' -> '+imgCount+' images found in '+id); 

    } 


}); 

我改變了搜索的代碼,以確保所有可能的類都被檢查過。用來開始檢索的舊代碼爲

 for (i=0; i<findImagesIn.length; i++) { 
      console.log(appname+' -> Looking for images in: '+findImagesIn[i]+'...'); 
      bfxView(findImagesIn[i]); 
     } 

更新的代碼不過在Chrome沒有要去與編輯上破Firefox瀏覽器。這會告訴我「寬度」和「高度」沒有定義,就像沒有加載圖像一樣。

  $(this).find('img').each(function() { 
       $(this).load(function(){ 
        var img = $(this), 
         width, height, origWidth = $(this).outerWidth(); 
         hiddenImg = img.clone().attr('class','').attr('id','').css('visibility', 'hidden').removeAttr('height').removeAttr('width').appendTo('#loading-images'); 
        height = hiddenImg.height(); 
        width = hiddenImg.width(); 
        hiddenImg.remove(); 
       }); 
+0

鉻***不是Firefox瀏覽器。 – WASasquatch 2014-12-08 06:32:46

回答

3

您的例子不工作:

BFX View -> 0 images found in ... 

無論如何,你試圖加載圖像之前讀取寬度。看一看答案爲這個問題,實際上涵蓋了同一主題:

Get the real width and height of an image with JavaScript? (in Safari/Chrome)

對於龐大的圖像,我建議使用服務器端腳本讀取圖像尺寸,所以客戶不必等待加載所有圖像來激活你的腳本。

+0

服務器端不是一個選項。如果在bbcode中沒有給出鏈接並且不可點擊開始,則大多數圖像受到限制。所以這只是一個額外的功能,否則將是一個陳舊的形象。當它工作,並且它在firefox中工作的很好時,圖像加載時真的存在滯後。 – WASasquatch 2014-12-16 07:45:46

+1

@WASasquatch他聯繫的答案完美。一探究竟。 – Makaze 2014-12-22 06:39:06

3

您需要爲所有圖像添加事件偵聽器,並在加載後得到它們的邊界>> for ex。

img.onload = function() { 
    // get width and height of the img refering to it using "this" keyword 
} 

,如果你正在使用JQuery

$(imgSelector).load(function(){ 
    // get width and height of the img refering to it using "$(this)" 
}): 
+0

我試過這樣做,而不是在Chrome或Firefox中無法使用。 xD – WASasquatch 2014-12-16 18:43:57

+0

任何想法。請參閱上面編輯的代碼和實時版本。 – WASasquatch 2014-12-18 19:01:07