jQuery图像调整大小 - 宽度不适合

问题描述:

我想制作一个横幅,您可以调整大小并拖动图像。除了调整大小外,一切工作都正常。jQuery图像调整大小 - 宽度不适合

当您使用图像下方的滑块调整图像大小时。图像将会增长。调整图像大小后,您可以成功将其拖到右侧和底部。当您将图像拖到右侧时,您决定再次将图像缩小。您可以使用图像下方的滑块。但现在出现了这个问题。如果您位于图像右侧,并且想要再次调整图像大小,图像将不适合div。你会看到黑色的背景。

这里是例子: http://jsfiddle.net/DennisBetman/tnaGA/

HTML:

<div id="box"> 
    <img src="http://www.digindigin.com/blog/wp-content/uploads/2012/05/Diablo_3___Wizard_Wallpaper_by_Lythus.jpg" id="img" width="371" height="auto" style="top:-0px; left:-0px;" /> 
</div> 
<!-- style="top:-262px; left:-425px;" --> 
<div id="zoom"></div> 
<input type="hidden" id="amount-width" style="border: 0; color: #f6931f; font-weight: bold;" /> 
<input type="hidden" id="amount-height" style="border: 0; color: #f6931f; font-weight: bold;" /> 

<div class="text"></div> 

<p> 
    Position x: 
    <input type="text" id="val-x" style="border:0; color:#f6931f; font-weight:bold;" /> 
</p> 
<p> 
    Position y: 
    <input type="text" id="val-y" style="border:0; color:#f6931f; font-weight:bold;" /> 
</p> 

的jQuery:

jQuery("#zoom").slider({ 
     max: 650, 
     slide: function(){ 

     var sliderValue = jQuery("#zoom").slider("value"); 
     jQuery("#img").width(370 + sliderValue); 

     $("#amount-width").val(jQuery("#img").css("width")); 
     $("#amount-height").val(jQuery("#img").css("height")); 

     var width = $("#img").width(); 
     var widthZoom = width + sliderValue; 
     var widthVerschil = widthZoom - sliderValue; 
     var totalWidth = widthVerschil - '373'; 

     var stageWidth = $("#box").width(); 

     var height = $("#img").height(); 
     var totalHeight = height - '207'; 

     $("#img").draggable({ 
      containment: [-totalWidth, -totalHeight, 0, 0], 
      scroll: false, 
      iframeFix: true, 
     }); 
     $('.text').html('<br/>The file size = ' + height + ' x ' + widthVerschil); 
     } 


}); 

var height = $("#img").height(); 
var totalHeight = height - '207'; 

$("#img").draggable 
({ 
    containment: [0, -totalHeight, 0, 0], 
    snap: false, 
    drag: function(event, ui) 
    { 
     $("#val-x").val(ui.position.left); 
     $("#val-y").val(ui.position.top); 

    } 

}); 

$("#img").offset({left: $(this).attr('position')}); 

CSS:

div { 
    width:370px; 
    height:204px; 
    position:relative; 
    overflow:hidden; 
    border-top-left-radius: 7px; 
    border-top-right-radius: 7px; 
} 

#box{ 
    background:black; 
} 


#img{ 
} 

我希望有人能帮助我。

问候, 丹尼斯。

这些行我添加了一些代码在你的小提琴,我认为这似乎是现在的工作...
小提琴:http://jsfiddle.net/tnaGA/28/

前滑块初始化:

var previousValue = 0; 

为滑动事件的事件处理函数中:

 if (sliderValue < previousValue){ 
      var t, l; 
      t = $('#img').position().top; 
      l = $('#img').position().left; 
      if(t < 0) 
       t = t + (previousValue - sliderValue); 
      if(l<0) 
       l = l +(previousValue - sliderValue); 
      $('#img').offset({top: t, left: l}); 
     } 

为了调试,你可以使用滑块的变化事件。因为当调整图像大小时,x和y值现在不会更新。

我希望这至少能激励你! ;)

+0

非常感谢你的工作。这正是我需要的。是的,这启发了我! :) 谢谢!! – 2013-05-03 16:30:43

+0

你真棒的东西只能在jsfiddle中出于某种原因。它不适用于JS BIN或仅在我的网站上。你知道别的吗? – 2013-05-04 13:32:25

+0

NVM再次工作;) – 2013-05-04 14:42:26

也许尝试这样的事情还是沿着你的幻灯片功能”

$('#img').css('top','1px'); 
$('#img').css('left','1px'); 
+0

感谢您的回答。我已经试过了。但是,当你用滑块缩小。它会自动进入顶部1px并保留1px。这不是我想要的。我希望一个人可以调整他们的位置。 – 2013-05-03 13:54:04