//Dynamically getting the windows width
var windowwidth = $(window).width() - 50;
//dynamically assigning the windowwidth value to the class(dynamically)
$(window).load(function () {
$('.cntnt').css('width', windowwidth + 'px');
})
$("#menu-toggle").click(function(e) {
e.preventDefault();
$('.wrapper-content').toggleClass('cntnt');
});
#wrapper {
padding-left: 0;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
}
#wrapper.toggled {
padding-left: 250px;
}
#sidebar-wrapper {
z-index: 1000;
position: fixed;
left: 250px;
width: 0;
height: 100%;
margin-left: -250px;
overflow-y: auto;
background: #4a4f55;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
}
#wrapper.toggled #sidebar-wrapper {
width: 250px;
}
#page-content-wrapper {
width: 100%;
position: absolute;
padding: 15px;
}
#wrapper.toggled #page-content-wrapper {
position: absolute;
margin-right: -250px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >
<div class="wrapper-content">
<div class="container-fluid">
<a href="#menu-toggle" class="btn pull-left" id="menu-toggle" >
toggle menu
</a>
<div class="row">
<div class="col-lg-12">
<h1>Simple Sidebar</h1>
<p>This template has a responsive menu toggling system. The menu will appear collapsed on smaller screens, and will appear non-collapsed on larger screens. When toggled using the button below, the menu will appear/disappear. On small screens, the page content will be pushed off canvas.</p>
<p>Make sure to keep all page content within the <code>#page-content-wrapper</code>.</p>
</div>
</div>
</div>
</div>
嗨,我正在尝试在jQuery中创建一个css类,并将其分配给jQuery中的另一个id。主要议程是,我想在jQuery中切换一个类,该类的宽度为$(window),在单击应将类切换为id的函数时,
这是我实现的代码,没有错误,但也没有输出。请帮帮我。
$(window).load(function () {
$('.cntnt').css('width', + windowwidth + 'px');
})
这不是类分配,您只需找到带有该类的元素并设置它们的样式即可。
如果要动态设置类,则应创建新的样式元素,例如:
$(window).load(function () {
$('body').prepend('<style>.cntnt { width:' + windowwidth + 'px }</style>')
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句