2011-08-31 41 views
4

我正在使用jCrop預覽演示來做我自己的事情。但是我遇到了一個問題。如果我使用setSelect:加載圖像時創建默認選擇,那麼我將選擇映射到大圖像上,但它不會出現在預覽中。當jCrop加載時,我無法找到調用updatePreview函數的api處理程序。有沒有人知道如何在jCrop加載時調用這個函數?jCrop - 更新預覽onload?

jQuery(function($){ 

     // Create variables (in this scope) to hold the API and image size 
     var jcrop_api, boundx, boundy; 

     $('#target').Jcrop({ 
     onChange: updatePreview, 
     onSelect: updatePreview, 
     setSelect: [ 0, 0, selectWidth, selectHeight ], 
     aspectRatio: 1 
     },function(){ 
     // Use the API to get the real image size 
     var bounds = this.getBounds(); 
     boundx = bounds[0]; 
     boundy = bounds[1]; 
     // Store the API in the jcrop_api variable 
     jcrop_api = this; 
     }); 

     function updatePreview(c) 
     { 
     if (parseInt(c.w) > 0) 
     { 
      var rx = 100/c.w; 
      var ry = 100/c.h; 

      $('#preview').css({ 
      width: Math.round(rx * boundx) + 'px', 
      height: Math.round(ry * boundy) + 'px', 
      marginLeft: '-' + Math.round(rx * c.x) + 'px', 
      marginTop: '-' + Math.round(ry * c.y) + 'px' 
      }); 
     } 
     }; 

    }); 

回答

3

我試圖設置默認值,並且預覽框中預覽的默認值。

jQuery(window).load(function(){ 

      jQuery('#cropbox').Jcrop({ 
       onChange: showPreview, 
       onSelect: showPreview, 
       setSelect: [ 100, 0, 100, 100 ], 
       aspectRatio: 1 
      }); 

     }); 

這只是我想要的方式。您的腳本可能還有其他錯誤。但是你不必調用任何特別的東西來顯示預覽。如果你沒有這樣做,那就試試它。

+0

好哆嗦我木材。這很煩人。感謝您指出我在正確的方向 – JasonS

5

我無法得到這個工作,並發現你自己的問題尋找解決方案。

我在Jcrop v0.9.9中使用預覽演示(tutorial3)。

改變jQuery(function($){jQuery(window).load(function(){

,並添加選項setSelect: [ 0, 40, 312, 244 ]

而且,它不會更新加載預覽..

我已經找到了解決辦法是使用API​​ animateTo以及setSelect(或者如果您喜歡動畫,可以使用swingSpeed更改其速度)

我所做的改變在你的代碼

jQuery(function($){ 

    // Create variables (in this scope) to hold the API and image size 
    var jcrop_api, boundx, boundy; 

    $('#target').Jcrop({ 
    onChange: updatePreview, 
    onSelect: updatePreview, 
    setSelect: [ 0, 0, selectWidth, selectHeight ], 
    aspectRatio: 1 
    },function(){ 
    // Use the API to get the real image size 
    var bounds = this.getBounds(); 
    boundx = bounds[0]; 
    boundy = bounds[1]; 
    // Store the API in the jcrop_api variable 
    jcrop_api = this; 
    jcrop_api.animateTo([ 0, 0, selectWidth, selectHeight ]); 
    }); 

    function updatePreview(c) 
    { 
    if (parseInt(c.w) > 0) 
    { 
     var rx = 100/c.w; 
     var ry = 100/c.h; 

     $('#preview').css({ 
     width: Math.round(rx * boundx) + 'px', 
     height: Math.round(ry * boundy) + 'px', 
     marginLeft: '-' + Math.round(rx * c.x) + 'px', 
     marginTop: '-' + Math.round(ry * c.y) + 'px' 
     }); 
    } 
    }; 

}); 
+0

這似乎是唯一列出的答案,實際上完全解決問題(正確更新預覽,還調用onChange回調,以便調用代碼知道什麼初始矩形JCrop選擇 - 因爲它會實際上修改您傳入setCoords/animateTo的座標,如果它們不匹配長寬比或適合圖像) – Yetanotherjosh

+0

真棒....感謝 – huzefam

+0

是的,這似乎工作。謝謝。 –

8

而非animateTo黑客,你可以只設置在回調初始選擇。所以:

$('#target').Jcrop({ 
    onChange: updatePreview, 
    onSelect: updatePreview, 
    aspectRatio: 1 
    },function(){ 
    // Use the API to get the real image size 
    var bounds = this.getBounds(); 
    boundx = bounds[0]; 
    boundy = bounds[1]; 
    // Store the API in the jcrop_api variable 
    jcrop_api = this; 
    jcrop_api.setSelect([ 0, 0, selectWidth, selectHeight ]); // <-- 
    }); 
+0

感謝您的支持,完美工作! – INT

+0

永遠感激不盡!正是我在網上搜索的! – LITguy

+0

這個也適用。謝謝。 –