oz1*_*1cz 5 javascript jquery twitter-bootstrap
从JavaScript程序中,我希望能够判断Bootstrap是否将当前显示大小视为xs,sm,md或lg.
这有效:
在HTML中写这个:
<p class="visible-xs-block xyzzy" data-size="xs"></p>
<p class="visible-sm-block xyzzy" data-size="sm"></p>
<p class="visible-md-block xyzzy" data-size="md"></p>
<p class="visible-lg-block xyzzy" data-size="lg"></p>
Run Code Online (Sandbox Code Playgroud)
然后在JavaScript(用JQuery)中,这个表达式
$('.xyzzy:visible').attr('data-size')
Run Code Online (Sandbox Code Playgroud)
返回xs,sm,md或lg.
这有效,但在我看来,这是一种相当笨拙的方式.
有更简单的方法吗?
使用最新版本(响应式引导工具包 2.5.0):
(function($, viewport){
// Executes only in XS breakpoint
if( viewport.is('xs') ) {
// ...
}
// Executes in SM, MD and LG breakpoints
if( viewport.is('>=sm') ) {
// ...
}
// Executes in XS and SM breakpoints
if( viewport.is('<md') ) {
// ...
}
// Execute only after document has fully loaded
$(document).ready(function() {
if( viewport.is('xs') ) {
// ...
}
});
// Execute code each time window size changes
$(window).resize(
viewport.changed(function(){
if( viewport.is('xs') ) {
// ...
}
})
});
})(jQuery, ResponsiveBootstrapToolkit);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2368 次 |
| 最近记录: |