[英]Padding change via JS
I need to change padding-bottom from -50% to -40% so, here is my CSS code 我需要将padding-bottom从-50%更改为-40%,所以这是我的CSS代码
.mid {
background-color: rgba(255,205,147,0.68);
border-radius: 25px;
margin-top: 18%;
margin-right: 10%;
margin-left: 10%;
padding-top: 50%;
padding-bottom: -40%;
background-attachment: fixed;
box-shadow: 9px -12px 5px rgba(0,0,0,0.5);
}
and here is my JS code 这是我的JS代码
var expandMid = document.getElementsByClassName("mid");
function buttonReady() {
if (expandMid.style.padding-bottom == -40%)
{
mid.style.padding-bottom == -30%;
}
else {
mid.style.padding-bottom = -40%;
}
}
Here is what I can see in browser console 这是我在浏览器控制台中看到的
Failed to load resource: net::ERR_FILE_NOT_FOUND
scripts.js:3 Uncaught SyntaxError: Unexpected token )
8index.html:72 Uncaught ReferenceError: buttonReady is not defined
at HTMLButtonElement.onclick (index.html:72)
As dashes are not legal in javascript identifiers, you need to use camelCase instead of kebab-case for style names, as in: 由于破折号在javascript标识符中是不合法的,因此您需要使用camelCase而不是kebab-case作为样式名称,如下所示:
expandMid.style.paddingBottom
Also, you seem to have your variable names wrong. 另外,您的变量名似乎错误。 Sometimes, the element is referred to as
expandMid
, sometimes as mid
有时,该元素称为
expandMid
,有时称为mid
Padding-bottom is a computed property from your css, therefore, you won't be able to access padding-bottom via elem.style.paddingBottom. 填充底部是CSS的计算属性,因此,您将无法通过elem.style.paddingBottom访问填充底部。
I would recommend this method so that it is cross-browser compatible. 我建议使用此方法,以便它与跨浏览器兼容。
$(document).ready(function() { var elem = $('.block'); // access paddingBottom like so... // console.log(elem.css('paddingBottom')); $('#set-padding').on('click', function() { var newPaddingBottomValue = '100px'; if (elem.css('paddingBottom') !== '50px') { newPaddingBottomValue = '50px'; } elem.css('paddingBottom', newPaddingBottomValue); }); });
.block { width: 10px; height: 10px; background: black; padding-bottom: 50px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="block"></div> <h5>some heading to show padding changes</h5> <button id="set-padding">Toggle padding</button>
This is not supported in IE8 or below! IE8或更低版本不支持此功能!
(function() { var elem = document.getElementById('block'); var btn = document.getElementById('toggle-padding'); btn.addEventListener('click', function() { var newPaddingBottomValue = '100px'; if (window.getComputedStyle(elem).paddingBottom !== '50px') { newPaddingBottomValue = '50px'; } elem.style.paddingBottom = newPaddingBottomValue; }); })();
#block { width: 10px; height: 10px; background: black; padding-bottom: 50px; }
<div id="block"></div> <button id="toggle-padding">Toggle padding</button>
You're getting a syntax error. 您收到语法错误。 Because in JS you can't use some special characters and assume they will just work.
因为在JS中,您不能使用某些特殊字符并假设它们会起作用。 So, the
-
(minus) character is a special math operator. 因此,
-
(减号)字符是特殊的数学运算符。 That you can use in CSS
, but not in JS! 您可以在
CSS
中使用,但不能在JS中使用!
So, if you want to access something that has a special character(like the style properties of the css), write the property as camel case. 因此,如果您要访问具有特殊字符的内容(例如CSS的样式属性),请将该属性编写为驼峰式大小写。
expandMid.style.paddingBottom // would give you the access.
Note that the value of
paddingBottom
should be a string.请注意,
paddingBottom
的值应为字符串。 Not a number.没有数字。 So, for comparsion use:
if (expandMid.style.paddingBottom === '40%') ...
因此,为了进行比较,使用:
if (expandMid.style.paddingBottom === '40%') ...
Here's why you can't apply a negative padding value source
这就是为什么您不能应用负填充值源的原因
Let me know if that helped! 让我知道是否有帮助!
Hope this work. 希望这项工作。 You were saving "mid" ref in "expandMid" but using "mid" directly.
您将“ mid”参考保存在“ expandMid”中,但直接使用了“ mid”。
var expandMid = document.getElementsByClassName("mid");
function buttonReady() {
if (expandMid.style.paddingBottom == -40%){
expandMid.style.paddingBottom == -30%;
}
else {
expandMid.style.paddingBottom = -40%;
}
}
I see at least three things wrong with your code: 我发现您的代码至少存在三件事:
buttonReady();
buttonReady();
运行它buttonReady();
-30%
and -40%
inside of quotation marks. -30%
和-40%
放在引号内。 The -
character is getting treated by Javascript as the subtraction operator, and the %
character is getting treated as the modulo operator. -
字符视为减法运算符,将%
字符视为模运算符。 If you're trying to compare strings, you need to treat them as such. I also see a couple of things wrong with the way you are trying to solve the problem: 我还发现您尝试解决问题的方式有几处错误:
getElementsByClassName
will return a list of things, even if you only have one of those things on your page. getElementsByClassName
也会返回一系列内容。 you need to either provide a different input to that function, or use a different function, or need to mid
in some places, but expandMid
in other places. mid
的变量,但在其他地方有expandMid
。 this is valid Javascript code (since variables not declared ahead of time will be assumed to be attached to window
) but will result in an error when you run it because that variable does not exist on the window. window
),但是在运行它时将导致错误,因为该变量在窗口中不存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.