2009-11-24 175 views
0

我不知道這是可以做到,但你可以使用一個變量在這種情況下使用:添加變量到一個jQuery插件

這是正常的方式:

jQuery(function() { 
    jQuery('#cropbox').Jcrop({ 
     onChange: showCoords, 
     onSelect: showCoords, 
     aspectRatio: 16/9 
    }); 
}); 

想這樣做像:

var ratioRetrieve = "16/9"; 
var run = true; 
if(run === true) { 
    jQuery(function(ratioRetrieve) { 
     jQuery('#cropbox').Jcrop({ 
      onChange: showCoords, 
      onSelect: showCoords, 
      aspectRatio: ratioRetrieve 
     }); 
    }); 
} 

編輯:這是完整的代碼

<script language="Javascript"> 
     $(document).ready(function(){ 
      /**************************** ASPECT RATIO ********************************/ 
      function getAspectRation(w, h){ 
       var rem; 
       var newW = w; 
       var newH = h;   
       while (h != 0) { 
        rem = w % h; 
        w = h; 
        h = rem; 
       }   
       newH = newH/w; 
       newW = newW/w;    
       return newW/newH; 
      } 
      function calcRatio(){ 
       $(".heightAndWidth").keypress(function (e) { 
        var newW = $("#outputImageWidth").val(); 
        var newH = $("#outputImageHeight").val(); 
        var ratioRetrieve = getAspectRation(newW, newH);  
        return ratioRetrieve; 
       }); 
      } 
      function calcRatio2(){ 
       var newW = $("#outputImageWidth").val(); 
       var newH = $("#outputImageHeight").val(); 
       var ratioRetrieve = getAspectRation(newW, newH); 
       return ratioRetrieve; 
      } 
      /**************************** END ASPECT RATIO ********************************/ 
      /**************** REPLACE TO MAKE ALL NON MOVABLE ANCHORS *********************/ 
      function anchorChange(){ 
       $("a").each(function(i){ 
        var anchorElement = $(this); 
        var newAnchorElement = $('<a href="#link01' + i + '" id="' + anchorElement.attr('id') + '" class="' + anchorElement.attr('class') + '" name="#link01' + i + '">' + anchorElement.text() + '</a>').insertBefore(anchorElement); 
         anchorElement.remove(); 
       }); 
      } 
      /************** REPLACE TO MAKE ALL NON MOVABLE ANCHORS END *******************/ 
      /**************************** CLOSE PREVIEW BUTTON ****************************/ 
      function closePreview(){ 
       $("#closePreviewButton").bind("click", function(){ 
        $("#cropResult").animate({ 
         height: "0px"          
        }, 1000, 
        function(){ 
         $("#cropResult").remove(); 
        }); 
       }); 
      } 
      /*********************** END CLOSE PREVIEW BUTTON ****************************/ 
      /************************** GET HEIGHT AND WIDTH *****************************/ 
      function getHeightOrWidth(){ 
       $("#getHeight").bind("click", function(){ 
        var heightOrWidthVal = $("#outputImageHeight").val(); 
        var newImagejCrop = $("#filename").val(); 
        var folderjCrop = $("#hiddenFolder").val(); 
        var imageDir = folderjCrop + newImagejCrop; 
        $("#heightWidthLoad").html('<p>Retrieving height... <img src="../../loader.gif" /></p>'); 

        $.ajax({ 
         type: "POST", 
         data: 'dir=' + imageDir + '&heightOrWidth=height', 
         url: 'https://web111.secure-secure.co.uk/snowyswebsolutions.co.uk/ACTfileUploader/imageCrop/func.getSize.php', 
         cache: false, 
         timeout: 12000, 
         error: function(XMLHttpRequest, textStatus, errorThrow){ 
          $("#heightWidthLoad").html(
          "<span class=\"red\">Error: Network connection low, please try again later.</span>" 
         );}, 
         success: 
         function(html){ 
          $("#outputImageHeight").val(html); 
          ratioRetrieve = calcRatio2(); 
          $("#heightWidthLoad").html(''); 
         } 
        }); 
       }); 
       $("#getWidth").bind("click", function(){ 
        var heightOrWidthVal = $("#outputImageWidth").val(); 
        var newImagejCrop = $("#filename").val(); 
        var folderjCrop = $("#hiddenFolder").val(); 
        var imageDir = folderjCrop + newImagejCrop; 
        $("#heightWidthLoad").html('<p>Retrieving width... <img src="../../loader.gif" /></p>'); 

        $.ajax({ 
         type: "POST", 
         data: 'dir=' + imageDir + '&heightOrWidth=width', 
         url: 'https://web111.secure-secure.co.uk/snowyswebsolutions.co.uk/ACTfileUploader/imageCrop/func.getSize.php', 
         cache: false, 
         timeout: 12000, 
         error: function(XMLHttpRequest, textStatus, errorThrow){ 
          $("#heightWidthLoad").html(
          "<span class=\"red\">Error: Network connection low, please try again later.</span>" 
         );}, 
         success: 
         function(html){ 
          $("#outputImageWidth").val(html); 
          ratioRetrieve = calcRatio2(); 
          $("#heightWidthLoad").html(''); 
         } 
        }); 
       }); 
      } 
      /************************** GET HEIGHT AND WIDTH EMD **************************/ 
      /**************** REMOVE AND INSERT JCROP WITH IMAGE CHANGE *******************/ 
      function imageChangejCrop(ratioRetrieve){ 
       run = false; 
       $("#imageList").change(function(ratioRetrieve){ 
        var newImagejCrop = $("#imageList :selected").text(); 
        var folderjCrop = $("#hiddenFolder").val(); 
        $(".jcrop-holder").remove(); 
        $("#cropbox").remove(); 
        $("#imageWrapper").html("<img src=\"" + folderjCrop + newImagejCrop + "\" id=\"cropbox\" />"); 
        $("#filename").val(newImagejCrop); 
        run = true; 
        jQuery('#cropbox').Jcrop({ 
         onChange: showCoords, 
         onSelect: showCoords, 
         aspectRatio: parseInt(ratioRetrieve) 
        }); 
        function showCoords(c){ 
         jQuery('#x').val(c.x); 
         jQuery('#y').val(c.y); 
         jQuery('#x2').val(c.x2); 
         jQuery('#y2').val(c.y2); 
         jQuery('#w').val(c.w); 
         jQuery('#h').val(c.h); 
        }; 

        function checkCoords(){ 
         if (parseInt(jQuery('#w').val())) return true; 
         alert('Please select a crop region then press submit.'); 
         return false; 
        }; 
       }); 
      } 
      /************** REMOVE AND INSERT JCROP WITH IMAGE CHANGE END ***************/ 

      imageChangejCrop(); 
      closePreview(); 
      anchorChange(); 
      getHeightOrWidth(); 
      ratioRetrieve = calcRatio(); 

     }); 

     /***************************************************************************/ 

     jQuery(window).load(function() { 
      var run = true; 
      if(run === true) { 
       jQuery(function(ratioRetrieve) { 
        jQuery('#cropbox').Jcrop({ 
         onChange: showCoords, 
         onSelect: showCoords, 
         aspectRatio: parseInt(ratioRetrieve) 
        }); 

       }); 
      } 
      function showCoords(c){ 
       jQuery('#x').val(c.x); 
       jQuery('#y').val(c.y); 
       jQuery('#x2').val(c.x2); 
       jQuery('#y2').val(c.y2); 
       jQuery('#w').val(c.w); 
       jQuery('#h').val(c.h); 
      }; 
      function checkCoords(){ 
       if (parseInt(jQuery('#w').val())) return true; 
       alert('Please select a crop region then press submit.'); 
       return false; 
      }; 
     }); 
    </script> 

回答

0

只要變量的範圍可以從調用它的地方訪問,就不會有問題。

+0

謝謝 - 去那裏... – 2009-11-24 16:37:24

0

你可以,但在你發佈的代碼片段ratioRetrieve是一個字符串,而不是一個整數(而「16/9」與16/9不一樣)。您可以使用它像var ratioRetrieve = 16/9;

+0

你的權利測試它......但我如何使這返回一個整數? function getAspectRation(w,h){ \t \t \t \t \t var rem; \t \t \t \t \t var newW = w; \t \t \t \t \t var newH = h; \t \t \t \t \t \t \t \t while(h!= 0){ \t \t \t \t \t \t rem = w%h; \t \t \t \t \t \t w = h; \t \t \t \t \t \t h = rem; \t \t \t \t \t \t} \t \t \t \t \t \t \t newH = newH/W; \t \t \t \t \t newW = newW/w; \t \t \t \t \t \t \t \t return newW/newH; \t \t \t \t} – 2009-11-24 16:38:06

0

@Phil傑克遜評論亞歷山大Gyoshev

確定getAspectRation應該返回一個整數?例如如果你這樣做16/9將返回1

我猜你真正想要的是浮動。但是你的函數應該返回一個浮點數而不用改變任何東西。

不然,如果你的地方有許多(不是表達式)的字符串,你需要它的數量做

var x = "0.5123"; 
return parseInt(x); //if you really want an integer 
return parseFloat(x); //if you want a float 

所以,如果你真的想getAspectRation返回一個整數,你可以做

return parseInt(newW/newH); 
+0

您可以使用'~~(newW/newH)'。 – Caleb 2009-11-24 16:53:59

+0

嗯,任何一個已經停止了它錯誤出來,但不工作... – 2009-11-24 16:55:53