[英]How do you increase element width as you decrease the window size?
I'm coding a website to fit both desktop and mobile, and of course the initial thought is to use a percentage width and then setup a mobile CSS stylesheet that changes the element width to 100% if the screen is under 700px. 我正在编写一个适合桌面和移动设备的网站,当然,最初的想法是使用百分比宽度,然后设置一个移动CSS样式表,如果屏幕小于700像素,则将元素宽度更改为100%。
The problem with that is the "clunk" that occurs when you cross the barrier. 这样做的问题是您越过障碍物时出现的“结块”。
So I thought, "Why not add a jQuery resize script that fluidly resizes the element to avoid the clunk?" 所以我想:“为什么不添加一个jQuery调整大小脚本来流畅地调整元素的大小,以免产生麻烦?”
The problem was writing the equation. 问题在于写方程式。
I figured it out, but now I'm worried about how much processing power it's using up. 我想通了,但是现在我担心它会消耗多少处理能力。 I don't want this feature to lag the rest of the website.
我不希望此功能落后于网站的其余部分。
Here's my jFiddle: http://jsfiddle.net/jp685suz/1/ 这是我的jFiddle: http : //jsfiddle.net/jp685suz/1/
...and the equation: ...以及等式:
var windowW = $(window).innerWidth();
var rangeMax = 1000; //Change occurs between 700px and 1000px
var rangeMin = 700;
var rangeDiff = 1000-700;
var minWidth = 50;
var dynWidth = Math.round(((1-(((rangeDiff-(rangeMax-windowW))/rangeDiff)/(100/(100-minWidth))))*100)*10)/10;
Try resizing the window to see what I mean. 尝试调整窗口大小以了解我的意思。 It's not laggy now, but add in a few high res photos in a slideshow and other jQuery functions and it isn't perfect.
现在还不算慢,但是在幻灯片放映和其他jQuery功能中添加了一些高分辨率照片,这并不完美。 Of course this isn't a huge issue because most people don't resize their window very often, but I like to show off.
当然,这不是一个大问题,因为大多数人并不经常调整窗口大小,但是我喜欢炫耀。
Can my equation be simplified? 我的方程式可以简化吗? Maybe there's something built-in to jQuery that does this exact thing?
也许jQuery内置了某些功能来执行此操作?
You can use CSS transitions for the effect you're after. 您可以使用CSS过渡来获得想要的效果。
transition: width 1s ease;
See your amended fiddle http://jsfiddle.net/3wtfenk2/ 看到您修改过的小提琴http://jsfiddle.net/3wtfenk2/
No javascript used. 没有使用JavaScript。
You want this: 你要这个:
100%
width when window is less than 700px
wide. 700px
时,宽度为100%
。 50%
width when window is more than 1000px
wide. 1000px
时,宽度为50%
。 Then, you decided to reduce the percentage linearly between 700px
and 1000px
. 然后,您决定在
700px
和1000px
之间线性减少百分比。
However, there is a problem: when the percentage is resolved, the used width becomes 但是,存在一个问题:解决百分比后,使用的宽度变为
As you can see, increasing the width of the window reduces the width of the section, producing a very weird effect. 如您所见,增加窗口的宽度会减小该部分的宽度,从而产生非常奇怪的效果。
Let me propose a different approach: 让我提出另一种方法:
100%
width when window is less than 700px
wide. 700px
时,宽度为100%
。 100% = 700px
width when window is 700px
wide. 700px
宽时, 100% = 700px
700px
宽。 700px
width when window is between 700px
and 1400px
wide. 700px
宽当窗口之间700px
和1400px
宽。 50% = 700px
width when window is 1400px
wide. 50% = 700px
宽度(当窗口为1400px
宽时)。 50%
width when window is more than 1400px
wide. 1400px
时,宽度为50%
。 That is, use a constant width of 700px
, and clamp it with percentages: 也就是说,使用
700px
的恒定宽度,并用百分比钳位它:
width: 700px;
min-width: 50%;
max-width: 100%;
section { width: 700px; min-width: 50%; max-width: 100%; margin: 0 auto; } section:before { content: ''; display: block; height: 100px; background-color: olivedrab; }
<section> <p>This olive green box will be a reasonable size on a big monitor (50% of the screen), and will fill the screen (100%) if it gets smaller than 700px (ie mobile).</p> <p>The transition is smooth. Go ahead, try resizing the window and see how pretty!</p> <p>This is not as "clunky" as setting a CSS mobile screen profile, but uses jQuery which isn't always a good idea.</p> </section>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.