基于计算定义css元素
defining css elements based on calculations
我想知道如何在呈现CSS元素维度之前定义它们,例如不使用偏移量和jQuery
我可以将php回声到宽度/高度或位置值的位置吗?例如
<?php
$width = 290px;
$altwidth = 290;
?>
<style>
.widget {
width: <?php echo $width; ?>;
}
.altwidget {
width: <?php echo $altwidth; ?>px;
}
这些都行得通吗,是不是完全错了?
类似地,我将如何使用变量应用JavaScript维度?
$(document).ready(function(){
$('.widget').css('width', <?php echo $width; ?>);
$('.altwidget').css('width', <?php echo $altwidth; ?>);
});
如果代码是用php文件编写的,那么它也可以工作。
是的,它会起作用,但您必须将290px
设置为$width
的字符串(例如$width = "290px";
),或者最好采用$altwidth
的方式,并将px
部分排除在变量之外。
请参阅以下内容,了解如何在页面加载后从JavaScript动态设置维度。我为这个例子添加了一些CSS。
<html>
<head>
<?php
$width = "290px"; // Quotes added
$altwidth = 290;
?>
<style>
.widget {
width: <?php echo $width; ?>;
height: 100px;
color: white;
background: green;
cursor: pointer;
}
.altwidget {
width: <?php echo $altwidth; ?>px;
height: 100px;
color: white;
background: red;
cursor: pointer;
}
</style>
<script type="text/javascript">
var newWidgetWidth = '200';
var newAltWidgetWidth = '400';
</script>
</head>
<body>
<div class="widget" onclick="this.style.width = newWidgetWidth + 'px';">
Shrink me
</div>
<div class="altwidget" onclick="this.style.width = newAltWidgetWidth + 'px';">
Grow me
</div>
</body>
</html>
相关文章:
- 使用自定义css在页面中弹出,而不覆盖页面's css
- 将自定义css保持在角度范围内
- 不使用自定义CSS或HTML(使用框架方法)的角度材质文本输入或文本区域标签大小
- Meteor:选择自定义javascript和自定义css作为模板
- 自定义 CSS 媒体打印查询中的模式高度和宽度
- 通过应用自定义 css 类禁用 html 输入元素
- 为html标记定义自定义css样式
- 自定义CSS复选框jQuery诱导的“;“已检查”;属性不't在视觉上”;“检查”;复选框
- wordpress主页上的自定义css和js
- 在条件中的几个时间行中设置自定义css效果后,从Jquery时间选择器显示时间
- 圆环饼图中数据标签的自定义CSS类
- CodeMirror在粘贴自定义css线条高度时呈现错误
- 基于计算定义css元素
- 自定义CSS输入框,使页面按ENTER键刷新
- 将自定义CSS注入WP Admin登录弹出iframe
- 如何将jQueryUI与我的自定义css一起使用
- 更改自定义css复选框的大小
- jQuery - 如果提交的表单为空,则添加自定义 css 类
- 将自定义 css 类添加到 ExtJS 4.0.7 面板标头
- 将自定义 CSS 类添加到由 kendoEditor 的工具栏操作触发的弹出窗口中