在jquery中创建和分配一个css类
Creating & Assigning a css class in jquery
//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 中切换类,它的宽度为 $(窗口(,单击一个函数,该类应该切换到 id,
这是我实现的代码,我没有错误,但也没有输出。请帮帮我。
$(window).load(function () {
$('.cntnt').css('width', + windowwidth + 'px');
})
这不是类分配,你只是找到这个类的元素并设置它们的样式。
如果你想动态设置类,你应该创建新的样式元素,如下所示:
$(window).load(function () {
$('body').prepend('<style>.cntnt { width:' + windowwidth + 'px }</style>')
})
为了动态创建 css 类,您可以创建一个 <style>
元素并将其附加到 <head>
。
$("<style>")
.prop("type", "text/css")
.html(".cntnt { width: " + windowwidth + "px; }")
.appendTo("head");
或者,如果你只需要 windowidth,你可以简单地在静态 css 中创建一个带有 vw
和 calc
的类。哪个100vw
等于$(window).width()
,calc
计算不同单位之间的值。通过这种方式,您可以在用户调整窗口大小时动态重新计算宽度。使用 javascript,您需要将代码放入事件$(window).resize
才能执行此操作。
.cntnt {
width: calc(100vw - 50px);
}
相关文章:
- 单击更改图标并通过javascript添加一个css类
- 用 LESS 伪造一个 css 属性(并在解析时用一个真实属性替换它)
- 在jquery中创建和分配一个css类
- 如何为每个html制作一个css
- I'我试图用一个按钮随意移动一个CSS元素,我试图使用偏移量,但onlick不起作用
- 创建一个CSS下拉菜单,其中包含从搜索图标弹出的搜索框
- 一种在另一个CSS值更改时更改CSS值的方法
- 在浏览器上存储一个 css 类
- 选择一个 CSS 选择器来动态设置元素的样式 ::before
- 当检测到另一个 CSS 更改时,使用 jQuery 或 MutationObserver 更改元素的 CSS
- 相对于另一个 css 元素定位一个元素,而不编辑 html
- 在 jQuery 中插入一个 CSS id
- 我创建了一个CSS切换器,但我只需要更改class=“”的元素;红色”;身体
- 使用JavaScript/jQuery从Bootstrap css中获取一个css值
- 事件侦听器,一个css转换结束,另一个开始
- 如何制作一个CSS类数组
- 在PhoneGap中只有一个CSS转换有效
- 如何将.vue文件中的所有css代码放在一个.css文件中
- 在xcode6中向UIWebView添加2个.js文件和一个.css文件
- 如何将页面上所有被屏蔽的谷歌广告(用图片)替换为第一个?CSS选择器出现问题