首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CSS UI:通过jQuery缩放内容时调整对象大小

问CSS UI:通过jQuery缩放内容时调整对象大小
EN

Stack Overflow用户
提问于 2013-06-25 05:27:23
回答 2查看 1.2K关注 0票数 1

我正在创建一种编辑器,并使用它来放大和缩小。css的作用是将主内容区域WHat缩放设置为用户指定的缩放比例。

当我集成时,我注意到jQUery UI .resizable不再根据鼠标位置来调整大小,而是在内容以100%的比例缩放时鼠标所在的位置。这是由于缩放百分比没有考虑在内,我假设。

JSFiddle:http://jsfiddle.net/qwMkt/

有什么办法可以解决这个问题吗?提前感谢

JS代码:

代码语言:javascript
复制
$(document).ready(function(){
    $('.box').resizable({
        containment: '#container',
        handles: "se",
        scroll: true
    });
});

CSS代码:

代码语言:javascript
复制
#container{
    zoom: 50%;
    background: #ccc;
    width: 500px;
    height: 500px;
}

#container .box {width: 100px; height: 100px; border: 1px solid #333}
EN

回答 2

Stack Overflow用户

发布于 2014-12-09 23:45:18

我曾遇到过类似的问题,但我想出了这个解决方案,使用鼠标的位置和当前的缩放。

此代码仅支持使用南向和东向句柄调整大小。如果要使用任何其他代码,则需要扩展代码。例如,我还实现了东南句柄的代码。但是,为了给出最低限度的要求,我省略了它:

代码语言:javascript
复制
$('.box').resizable({
   ...

   resize: function(event, ui) {
      var $element = ui.element;
      // Gets the axis that the user is dragging.
      var axis = $element.data('ui-resizable').axis;

      // Get the dimensions of the element's bounding box. We can't use 
      // leftOffset + $element.width() because it doesn't factor in the 
      // current zoom. Morever, we can't just multiply $element.width() 
      // by the current zoom to get the zoomed width because it still 
      // gives the wrong value. So we have to be sneaky and use resizable's 
      // resizer overlays to get the right and bottom values.
      var leftOffset = $element.offset().left;
      var topOffset = $element.offset().top;
      var rightOffset = $element.children('.ui-resizable-e').offset().left;
      var bottomOffset = $element.children('.ui-resizable-s').offset().top;

      // We are using the mouse location to calculate the width (during 
      // a horizontal resize) and the height (during a vertical resize) 
      // of the element. This is because we need the resize box to keep
      // up with the mouse when the user is not at 100% zoom.
      var mouseX = event.pageX;
      var mouseY = event.pageY;

      // Must remove the zoom factor before setting width and height.
      var mouseCalculatedWidth = (mouseX - leftOffset) / current_zoom;
      var mouseCalculatedHeight = (mouseY - topOffset) / current_zoom;
      var offsetWidth = (rightOffset - leftOffset) / current_zoom;
      var offsetHeight = (bottomOffset - topOffset) / current_zoom;

      // In order to maintain aspect ratio, we manually calculate it.
      var aspectRatio = offsetHeight / offsetWidth;

      // Resizing using the east handler, set the width of the element.
      // If this element maintains its aspect ratio, also set the height.
      if (axis == "e") {
         $element.width(mouseCalculatedWidth);

         if (maintainAspectRatio) {
            $element.height(mouseCalculatedWidth * aspectRatio);
         }
      }

      // Resizing using the south handler, set the height of the element.
      // If this element maintains its aspect ratio, also set the width.
      if (axis == "s") {
         $element.height(mouseCalculatedHeight);

         if (maintainAspectRatio) {
            $element.width(mouseCalculatedHeight / aspectRatio);
         }
      }
   }

   ...
});// Resizable
票数 0
EN

Stack Overflow用户

发布于 2017-07-27 02:37:15

如果我正确理解了您的问题,那么您应该在resizable(..)函数的参数中添加alsoResize: "#container"。

请参阅文档:http://api.jqueryui.com/resizable/#option-alsoResize

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17285420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档