在響應式布局逐漸成為主流的今天,網頁或者app的流式布局已經不算是一個新鮮的詞匯了。今天我要講的一個內容也是跟頁面流式布局有關,如何讓你的網頁實現完美的縮放?
我們可以很快速的寫出一個響應式布局的頁面,首先看一下效果圖。
網頁布局
其中html部分的代碼如下:
html部分代碼
css中的item代碼為:
css代碼
通過以上代碼,完成上述的流式布局后,在我們改變瀏覽器窗口大小時,div也會隨之進行縮放。
But,你以為這就是我們想要的結果嗎?
當然不是!在改變瀏覽器窗口大小時,我們發現雖然div的寬度是進行了縮放,但是高度卻沒變,因此div的寬高比并未保持原始比例,我們可以看下以下的效果。
縮放后寬高比
從上圖中可以很容易看出,縮放后的div寬高比比之前小很多,這并不是我們想要的結果。
我們需要達到的效果是在改變瀏覽器窗口大小時,div也會隨之進行等比例的縮放。
首先,可以使用Javascript代碼去實現,這是沒有問題的。但是綁定Javascript的onresize事件,在拖拽時可能會出卡頓現象,體驗不是很好。
接下來我們通過CSS來實現以上的效果。
使用的核心屬性是我們平時并不太注意的padding-bottom。
padding-bottom有一個很容易讓人忽略的特性是,當取值為百分比形式時,其百分比的基數是父元素的寬度,而不是高度。
因此我們可以在不用給父元素設置高度的時候,就可以通過padding-bottom屬性確定當前元素的高度。我們的做法如下。
將元素的height屬性設為0,通過padding-bottom屬性確定元素高度。
設置合理的padding-bottom值,例如上述的例子中,在寬度為21%時,如果需要高度是寬度的1.62倍,我們可以將padding-bottom取值為34%
修改后的CSS代碼如下。
修改后的CSS
修改后,我們再次調整瀏覽器窗口的大小,就會發現div是等比例的進行縮放,完美達到了我們的要求。
修改后等比例縮放
在這里,可能會有人有疑問如果設置overflow:hidden;那么里面的文字會不會因為超過height,就會被隱藏了?
答案是不會的,根據CSS2.1的規范,overflow只會對處于padding外面的內容生效,即只有超出了 padding區域的內容才會被overflow屬性隱藏掉。而在設置padding-bottom后,實際已經決定了元素的height屬性,因此overflow:hidden;不會生效。
今天這篇文章主要講解了利用CSS完成頁面等比例縮放的最簡單方式,你學會了嗎?
調整網頁頁面字體大小:
在瀏覽器打開一個頁面,同時按住鍵盤Ctrl鍵+鼠標滾輪(或者鍵盤“+/-”),即可調整網頁字體大小;
還有一種方法是點擊網頁上的工具欄圖標,點擊網頁縮放,選擇相應的字體大小進行調整。
(本文轉發自 http://www.mengniukeji.xyz/1/132.html 商業轉載請聯系作者獲得授權,非商業轉載請注明出處)
用鼠標改變元素的尺寸。
如需了解更多有關 resizable 交互的細節,請查看 API 文檔 可調整尺寸小部件(Resizable Widget)。
默認功能
在任意的 DOM 元素上啟用 resizable 功能。通過鼠標拖拽右邊或底邊的邊框到所需的寬度或高度。
<!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>jQuery UI 縮放(Resizable) - 默認功能</title> <link rel="stylesheet" > <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" > <style> #resizable { width: 150px; height: 150px; padding: 0.5em; } #resizable h3 { text-align: center; margin: 0; } </style> <script> $(function() { $( "#resizable" ).resizable(); }); </script></head><body> <div id="resizable" class="ui-widget-content"> <h3 class="ui-widget-header">縮放(Resizable)</h3></div> </body></html>
查看演示
動畫
使用 animate
選項(布爾值)使縮放行為動畫化。當該選項設置為 true 時,拖拽輪廓到所需的位置,元素會在拖拽停止時以動畫形式調整到該尺寸。
<!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>jQuery UI 縮放(Resizable) - 動畫</title> <link rel="stylesheet" > <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" > <style> #resizable { width: 150px; height: 150px; padding: 0.5em; } #resizable h3 { text-align: center; margin: 0; } .ui-resizable-helper { border: 1px dotted gray; } </style> <script> $(function() { $( "#resizable" ).resizable({ animate: true }); }); </script></head><body> <div id="resizable" class="ui-widget-content"> <h3 class="ui-widget-header">動畫</h3></div> </body></html>
查看演示
限制縮放區域
定義縮放區域的邊界。使用 containment
選項來指定一個父級的 DOM 元素或一個 jQuery 選擇器,比如 'document.'。
<!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>jQuery UI 縮放(Resizable) - 限制縮放區域</title> <link rel="stylesheet" > <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" > <style> #container { width: 300px; height: 300px; } #container h3 { text-align: center; margin: 0; margin-bottom: 10px; } #resizable { background-position: top left; width: 150px; height: 150px; } #resizable, #container { padding: 0.5em; } </style> <script> $(function() { $( "#resizable" ).resizable({ containment: "#container" }); }); </script></head><body> <div id="container" class="ui-widget-content"> <h3 class="ui-widget-header">限制</h3> <div id="resizable" class="ui-state-active"> <h3 class="ui-widget-header">縮放(Resizable)</h3> </div></div> </body></html>
查看演示
延遲開始
通過 delay
選項設置延遲開始縮放的毫秒數。通過 distance
選項設置光標被按下且拖拽指定像素后才允許縮放。
<!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>jQuery UI 縮放(Resizable) - 延遲開始</title> <link rel="stylesheet" > <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" > <style> #resizable, #resizable2 { width: 150px; height: 150px; padding: 0.5em; } #resizable h3, #resizable2 h3 { text-align: center; margin: 0; } </style> <script> $(function() { $( "#resizable" ).resizable({ delay: 1000 }); $( "#resizable2" ).resizable({ distance: 40 }); }); </script></head><body> <h3 class="docs">時間延遲 (ms):</h3><div id="resizable" class="ui-widget-content"> <h3 class="ui-widget-header">時間</h3></div> <h3 class="docs">距離延遲 (px):</h3><div id="resizable2" class="ui-widget-content"> <h3 class="ui-widget-header">距離</h3></div> </body></html>
查看演示
助手
通過設置 helper
選項為一個 CSS class,當縮放時只顯示元素的輪廓。
<!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>jQuery UI 縮放(Resizable) - 助手</title> <link rel="stylesheet" > <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" > <style> #resizable { width: 150px; height: 150px; padding: 0.5em; } #resizable h3 { text-align: center; margin: 0; } .ui-resizable-helper { border: 2px dotted #00F; } </style> <script> $(function() { $( "#resizable" ).resizable({ helper: "ui-resizable-helper" }); }); </script></head><body> <div id="resizable" class="ui-widget-content"> <h3 class="ui-widget-header">助手</h3></div> </body></html>
查看演示
最大/最小尺寸
使用 maxHeight
、maxWidth
、minHeight
和 minWidth
選項限制 resizable 元素的最大或最小高度或寬度。
<!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>jQuery UI 縮放(Resizable) - 最大/最小尺寸</title> <link rel="stylesheet" > <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" > <style> #resizable { width: 200px; height: 150px; padding: 5px; } #resizable h3 { text-align: center; margin: 0; } </style> <script> $(function() { $( "#resizable" ).resizable({ maxHeight: 250, maxWidth: 350, minHeight: 150, minWidth: 200 }); }); </script></head><body> <div id="resizable" class="ui-widget-content"> <h3 class="ui-widget-header">放大/縮小</h3></div> </body></html>
查看演示
保持縱橫比
保持現有的縱橫比或設置一個新的縱橫比來限制縮放比例。設置 aspectRatio
選項為 true,且可選地傳遞一個新的比率(比如,4/3)。
<!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>jQuery UI 縮放(Resizable) - 保持縱橫比</title> <link rel="stylesheet" > <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" > <style> #resizable { width: 160px; height: 90px; padding: 0.5em; } #resizable h3 { text-align: center; margin: 0; } </style> <script> $(function() { $( "#resizable" ).resizable({ aspectRatio: 16 / 9 }); }); </script></head><body> <div id="resizable" class="ui-widget-content"> <h3 class="ui-widget-header">保持縱橫比</h3></div> </body></html>
查看演示
對齊到網格
對齊 resizable 元素到網格。通過 grid
選項設置網格單元的尺寸(以像素為單位的高度和寬度)。
<!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>jQuery UI 縮放(Resizable) - 對齊到網格</title> <link rel="stylesheet" > <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" > <style> #resizable { width: 150px; height: 150px; padding: 0.5em; } #resizable h3 { text-align: center; margin: 0; } </style> <script> $(function() { $( "#resizable" ).resizable({ grid: 50 }); }); </script></head><body> <div id="resizable" class="ui-widget-content"> <h3 class="ui-widget-header">網格</h3></div> </body></html>
同步縮放
通過點擊并拖拽一個元素的邊來同時調整多個元素的尺寸。給 alsoResize
選項傳遞一個共享的選擇器。
<!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>jQuery UI 縮放(Resizable) - 同步縮放</title> <link rel="stylesheet" > <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" > <style> #resizable { background-position: top left; } #resizable, #also { width: 150px; height: 120px; padding: 0.5em; } #resizable h3, #also h3 { text-align: center; margin: 0; } #also { margin-top: 1em; } </style> <script> $(function() { $( "#resizable" ).resizable({ alsoResize: "#also" }); $( "#also" ).resizable(); }); </script></head><body> <div id="resizable" class="ui-widget-header"> <h3 class="ui-state-active">縮放</h3></div> <div id="also" class="ui-widget-content"> <h3 class="ui-widget-header">同步縮放</h3></div> </body></html>
查看演示
文本框
可縮放的文本框。
<!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>jQuery UI 縮放(Resizable) - 文本框</title> <link rel="stylesheet" > <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" > <style> .ui-resizable-se { bottom: 17px; } </style> <script> $(function() { $( "#resizable" ).resizable({ handles: "se" }); }); </script></head><body> <textarea id="resizable" rows="5" cols="20"></textarea> </body></html>
視覺反饋
通過設置 ghost
選項為 true,可在縮放期間顯示一個半透明的元素,而不是顯示一個實際的元素。
*請認真填寫需求信息,我們會在24小時內與您取得聯系。