简体   繁体   English

将div的大小调整为小于其声明的大小?

[英]Resize a div to smaller than its declared size?

Is there a way to resize a div to smaller than the declared height and width? 有没有办法将div的大小调整为小于声明的高度和宽度?

I'm making an application where the user can resize a div and save it. 我正在创建一个应用程序,用户可以调整div的大小并保存它。 However, when I load it later and set it to the size the user set to last time, they can make the div bigger, but not smaller. 但是,当我稍后加载它并将其设置为用户设置为上次的大小时,它们可以使div更大,但不能更小。

This is a sample div: 这是一个示例div:

.cls {
  border: solid 1px;
  width: 100px;
  height: 100px;
  resize: both;
  overflow: auto;   
}​

http://jsfiddle.net/FNXnD/ http://jsfiddle.net/FNXnD/

This question has the same issue but uses GWT and doesn't seem to have a good solution. 这个问题有同样的问题,但使用GWT并没有一个好的解决方案。

If you want to use the CSS3 UI resize feature, then your browser decides whether you can make it smaller or bigger than declared width or height. 如果要使用CSS3 UI调整大小功能,则浏览器会决定是否可以使其小于或大于声明的宽度或高度。 http://www.w3.org/TR/css3-ui/#resize http://www.w3.org/TR/css3-ui/#resize

The user agent may restrict the resizing range to something suitable, such as between the original formatted size of the element, and large enough to encompass all the element's contents. 用户代理可以将调整大小范围限制为合适的范围,例如在元素的原始格式化大小之间,并且足够大以包含所有元素的内容。

In Firefox for example, you can resize it smaller than your declared width or height. 例如,在Firefox中,您可以将其调整为小于声明的宽度或高度。 Whilst in chrome you cannot. 虽然在铬合金中你不能。

Just want to make a slight modification to what Pfft said in the accepted answer. 只想稍微修改Pfft在接受的答案中所说的内容。

If you use the :active pseudo-class instead of :hover it doesn't have the flicker when you hover over the element. 如果您使用:active伪类而不是:hover,当您将鼠标悬停在元素上时,它没有闪烁。 That flicker is really annoying. 那闪烁真的很烦人。

div:active {
  width: 0;
  height: 0;
}

By using :active what happens is when clicking on the element it will be resized to the height: 0, width: 0 so a little dot where the 1px border is will be shown but as soon as the user starts resizing you get exactly what you would want in resizing. 通过使用:active发生的事情就是点击元素时它将被调整为高度:0,width:0所以将显示1px边框的小点,但是一旦用户开始调整大小,你就会得到你的确切想要调整大小。 And on every subsequent resize there is no glitch and it just works perfectly. 并且在每次后续调整大小时都没有任何故障,它只是完美无缺。

I find this to be much less annoying than using hover. 我觉得这比使用悬停要烦恼得多。 Thanks to Pfft for the above response, it was exactly what I was looking for and led me to my own slightly altered solution! 感谢Pfft的上述回复,这正是我所寻找的,并引导我自己略微改变了解决方案!

Here's a jsfiddle of it: http://jsfiddle.net/kronenbear/v4Lq5o09/1/ 这是一个jsfiddle: http//jsfiddle.net/kronenbear/v4Lq5o09/1/

You requested a workaround for your problem, or was wondering how-to at least. 您要求为您的问题解决方法,或者至少想知道如何操作。 I made you a simple workaround that might require a little work; 我为你做了一个简单的解决方法,可能需要一些工作; note it is a workaround, and might therefore contain glitches. 请注意,这是一种解决方法,因此可能包含故障。

.cls {
    width: 100px;
    height: 100px;
    resize: both;
    overflow: auto;
    border: 1px dotted #000;
}

.cls:hover {
    width: 1px;
    height: 1px;
}

​ Updated fiddle: http://jsfiddle.net/FNXnD/1/ 更新小提琴: http//jsfiddle.net/FNXnD/1/

  • Note Chrome, and Safari might change the CSS3 resize feature in future versions. 注意 Chrome和Safari可能会在将来的版本中更改CSS3调整大小功能。

Or you can use jQuery-UI to make a resizable window. 或者您可以使用jQuery-UI创建可调整大小的窗口。

I went ahead and addressed some issues I experienced when trying to use the jQuery ui resizable widget with a div that had a scroll overflow. 我继续解决了我在尝试使用带有滚动溢出的div的jQuery ui可调整大小的小部件时遇到的一些问题。 The issue was that the resize handles were positioned inside of the scrollbars. 问题是调整大小手柄位于滚动条的内部。

To fix this I made three divs nested inside of each other 为了解决这个问题,我制作了三个嵌套在一起的div

The first one is the Wrapper and is where you set the starting size of the div. 第一个是Wrapper,您可以在其中设置div的起始大小。

The second one is for the handles and its size should be set to 100% of its parent div (the wrapper div). 第二个用于句柄,其大小应设置为其父div(包装div)的100%。 You will have to adjust the z-index to position them onto of the eventual scrollbars. 您必须调整z-index以将它们定位到最终滚动条上。

The second div should also contain the draggable handle div and the window div where all your windows content will be. 第二个div还应该包含可拖动的句柄div和窗口div,其中所有的窗口内容都将是。

Both the draggable handle div (.draggable) and the window div (.window) need to be positioned relatively. 可拖动的句柄div(.draggable)和窗口div(.window)都需要相对定位。 So that they are positioned properly with each other and so the draggable handle won't be over the top of your scrollbars. 这样它们就可以相互正确定位,因此可拖动的手柄不会超过滚动条的顶部。

The .window div should be scaled to 100% the parent div just as the div before. .window div应该缩放为父div的100%,就像之前的div一样。 This one will have a scroll overflow. 这个将有一个滚动溢出。 Now because this is equal to 100% the handles div when you resize the handle div with jQuery ui it will also resize this div. 现在因为当你使用jQuery ui调整句柄div时,这等于100%处理div,它也会调整这个div的大小。

Set the draggable div to a width of 100% and a height of whatever you want but any added height will push the window div down further then the handle div and you will need to add an equal amount of padding-bottom to the handle div to fix this. 将可拖动的div设置为100%的宽度和任何你想要的高度,但任何增加的高度都会将窗口div向下推到手柄div,你需要在句柄div上添加等量的padding-bottom解决这个问题。 You will also need to set the handles div overflow to visible. 您还需要将句柄div overflow设置为visible。

After all that add whatever styling you want to the different elements and you should be good. 毕竟,为了不同的元素添加你想要的任何样式,你应该是好的。 Its hard to explain but, I hope it makes sense. 很难解释,但我希望这是有道理的。 All the code is below and hopefully it helps explain better then my rambling. 所有的代码都在下面,希望它有助于解释我的漫无边际。 This will definitely allow you to resize the window smaller, move the div around and also have a scroll overflow with your div should you want it. 这肯定会让你调整窗口的大小,移动div,并且如果你想要它还有你的div的滚动溢出。

HTML HTML

<!DOCtype html>
<html lang="en">
<head>
    <title>Adjustable Window</title>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="../css/adjWindow.css">
    <link rel="stylesheet" type="text/css" href="../jquery-ui-1.11.4/jquery-ui.min.css">
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script type="text/javascript" src="../jquery-ui-1.11.4/jquery-ui.min.js"></script>
    <script type="text/javascript" src="../js/adjWin.js"></script>
</head>
<body id="top">
   <div class="winWrap">
        <div class="handles">
            <div class="draggable"><p>Window Title</p></div>
            <div class="window">
                <div class="windowContent">
                </div>
            </div>
        </div>
    </div>
</body>
</html>

CSS CSS

.winWrap {
    position: relative;
    width: 500px;
    height: 500px;
}
.draggable {
    position: relative;
    width: 100%;
    height: 20px;
    box-shadow: inset 0px 0px 6px rgba(0,0,0,0.9);
    background-color: rgba(255,255,255,0.5);
    text-align: center;
    z-index: 1;
}
.draggable p {
    padding: 2px;
    margin: 0px;
    cursor: default;
}
.handles {
    position: relative;
    width: 100%;
    height: 100%;
    padding-bottom: 20px;
    overflow: visible;
    box-shadow: 20px 15px 20px rgba(0,0,0,0.4);
    z-index: 1;
}
.window {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: scroll;
    background-color: rgba(0,0,0,0.1);
    box-shadow: inset 0px 0px 10px rgba(0,0,0,0.7);
}
.windowContent{
    position: absolute;
}

jQuery and jQuery-UI it's a good idea to use the minHeight and minWidth options with the resizable widget otherwise your window will be able to be resized to almost nothing and could cause some annoying side effects like making the div so small that you need to reload the page to click the resizing handles. jQuery和jQuery-UI使用minHeight和minWidth选项与可调整大小的小部件是一个好主意,否则你的窗口将能够调整到几乎没有任何东西,并可能导致一些恼人的副作用,如使div太小,你需要重新加载单击调整大小手柄的页面。 It also just looks more professional in my opinion. 在我看来,它看起来也更专业。

For the draggable widget you target the wrapper for the entire div you want to move around and then give it a handle of the draggable div we placed in our window div. 对于可拖动的小部件,您可以为要移动的整个div定位包装器,然后为它们提供我们放置在窗口div中的可拖动div的句柄。

/*global $, jQuery, alert*/
$(document).ready(function () {
    'use strict';
    $('.handles').resizable({minHeight: 100, minWidth: 100});
    $('.winWrap').draggable({handle: '.draggable'});
});
$('.cls').css('height', 200);
$(".cls").mouseup( function(){
var height = $(".cls").height();
var width = $(".cls").width();
if(height>=200){
$('.cls').css('height', 200);
}
if(width>=100){
$('.cls').css('width', 100);  
}
var
newdimensions="newheight="+height+"&"+"newwidth="+width;
});

In javascript usign jquery: 在javascript中使用jquery:

//get the current heigt and the width of the element
var h = $('.cls').height();
var w = $('.cls').width();
// i don'T know how you provide your nes size, but once you have in i a variable, you can just do
if(your_h > h) $('.cls').height(your_h);
if(your_w > w) $('.cls').width(your_w);

assuming you size are in PX. 假设您的尺寸在PX中。

Height() and width() will return the size in px if not arguments are specified. 如果未指定参数,Height()和width()将返回px中的大小。 They will set the height and width if you provide a value. 如果提供值,它们将设置高度和宽度。 If you don't explicitly specify a mesurment, px are used by default. 如果未明确指定mesurment,则默认使用px。

On the second part, it simply check if the new size (your_x) is biggre than the originla size, if so, it will resize it, if not it will jut ignore. 在第二部分,它只是检查新尺寸(your_x)是否大于原始尺寸,如果是这样,它将调整它的大小,如果不是,它将被忽略。

AND YES, jquery will overwide your css specification ;) anyone, biger or smaller values 和是,jquery将覆盖你的css规范;)任何人,更大或更小的值

use !important to override it from css: use!important从css覆盖它:

.cls {
  border: solid 1px;
  width: 100px !important;
  height: 100px !important;
  resize: both;
  overflow: auto;   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM