响应式调整大小的 jQuery

5

你好,我在使用jQuery进行响应式缩放时遇到了问题。我有动态的div,希望将它们移动到另一个div中。当屏幕尺寸发生变化时,我的divs宽度、高度和位置保持不变。如何解决这个问题?我希望在屏幕大小发生变化时,divs的大小也随之改变并保持原来的位置。我考虑使用$(window).resize(function(){});,但是具体怎么实现呢?有人可以帮助我吗?

$('.box').each(function() { $('.box').draggable({containment: '#boxid'});});
$('.box').each(function() { $('.box').resizable();});
#boxid {
    height: 750px;
    border: 5px dotted #292929;        
}

.box {
    background:rgb(107, 193, 243);
    cursor:move;
    position:absolute;
}
<link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    
<div class="row">
    <div id="boxid" class="col-xs-12">
    <!-- hier die Position änder -->
    </div>
</div>
<!--here I start the foreach -->
    <div id="id" class="box" style="top:50px; left:50px; width:50px; height:50px;">           
        <p id="top"></p>
        <p id="left"></p>  
        <p id="height"></p>
        <p id="width"></p>          
    </div>
        <div id="id" class="box" style="top:50px; left:50px; width:50px; height:50px;">           
        <p id="top"></p>
        <p id="left"></p>  
        <p id="height"></p>
        <p id="width"></p>          
    </div>
<!--here I end the foreach -->

这里有一个问题: enter image description here

你能否创建一个 jsfiddle? - SRK
@SmitRaval 这是链接 https://jsfiddle.net/cristiannn/ztf7pry6/38/ - user9438961
2个回答

0

你可以像这样做。

$(document).ready(function() {

$.ui.intersect = function () { return true; };

$('.box').each(function() {


$('.box').resizable();
});

$('.box').each(function() {

$(".box").draggable({ 
    revert: 'valid',  
    containment: '#lager',
        drag: function() {
                var top = $(this).offset().top;
                var left = $(this).offset().left;
                $("#top").html(top);
                $("#left").html(left);
                },
                  stop: function(e, ui) {
    var perc = ui.position.left / ui.helper.parent().width() * 100;
    ui.helper.css('left', perc + '%');
    }     
    }); 
});

$('.box').droppable({  tolerance: 'touch'});
});

将位置转换为百分比,用于“STOP”事件。这里有一个jsfiddle

但是位置和大小呢? - user9438961
我没听懂你说什么了?@CristianCănănău - SRK
当盒子div彼此靠近且屏幕大小改变时,一个div会重叠在另一个div上。而当你有一个大的div时,它将超出div的容器。 - user9438961

0

这里提供一种使用$(window).resize()的解决方案。

在回调函数中检查宽度/高度是否已更改,并根据更改调整.box元素的left/widthtop/height属性。

$(window).resize()的回调体被包装在一个setTimeout(...)中,以避免每次触发事件时执行代码 - 这会导致不精确的更改。

$('.box').each(function() { $('.box').draggable({containment: '#boxid'});});
$('.box').each(function() { $('.box').resizable();});

let boxH = $("#boxid").height(),
    boxW = $("#boxid").width(),
    doit;

$(window).on('resize', function() {

  clearTimeout(doit);
  
  doit = setTimeout(function() {
  
    let box  = $("#boxid"),
        newH = box.height(),
        newW = box.width();

    if (newH != boxH) {

      $('.box').each(function() {

        let prop = newH / boxH,
            self = $(this),
            sT   = Number(self.css('top').slice(0,-2)),
            sH   = Number(self.css('height').slice(0,-2));

        self.css({'top': sT * prop});
        self.css({'height': sH * prop});
        
        console.log("TOP/LEFT", self.css('top'), self.css('left'));

      });

    }

    if (newW != boxW) { 

      $('.box').each(function() {

        let prop = newW / boxW,
            self = $(this),
            sL   = Number(self.css('left').slice(0,-2)),
            sW   = Number(self.css('width').slice(0,-2));

        self.css({'left': sL * prop});
        self.css({'width': sW * prop});

        console.log("TOP/LEFT", self.css('top'), self.css('left'));
        
      });

    }

    boxH = newH;
    boxW = newW;
    
  }, 300);
});
#boxid {
    height: 750px;
    width: 50%;
    margin: 0 auto;
    border: 5px dotted #292929;        
}

.box {
    background:rgb(107, 193, 243);
    cursor:move;
    position:absolute;
}
<link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    
<div class="row">
    <div id="boxid" class="col-xs-12">
      <div id="id1" class="box" style="top:50px; left:50px; width:50px; height:50px;">           
          <p id="top"></p>
          <p id="left"></p>  
          <p id="height"></p>
          <p id="width"></p>          
      </div>
      <div id="id2" class="box" style="top:50px; left:50px; width:50px; height:50px;">           
          <p id="top"></p>
          <p id="left"></p>  
          <p id="height"></p>
          <p id="width"></p>          
      </div>
    </div>
</div>


谢谢您的帮助,这正是我需要的,但我仍然有一个问题,因为该框必须放在div行的外面,就像我的示例一样,这就是我遇到这个问题的原因https://i.stack.imgur.com/zcgUl.jpg。 - user9438961
然后只需将 .box 元素移出 #boxid - Kristianmitk
是的,我已经完成了这个,但我遇到了这个问题 https://i.stack.imgur.com/zcgUl.jpg - user9438961
抱歉,我不太明白你的问题 - 你想让.box元素在#boxid之外还是之内?这张图片应该告诉我什么? - Kristianmitk
我有一个div行,我需要将我的框移动到div之外。当我打开控制台时,我遇到了这个问题i.stack.imgur.com/zcgUl.jpg。我认为问题是因为你的代码中框在行内。 - user9438961
不要使用 $(window).width()/height(),而是使用 $("#boxid") 来解决这个问题。我已经编辑了上面的代码,并且改变了 #boxid 的宽度以使其更清晰。 - Kristianmitk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接