Can I use CSS calc within Javascript?

点点圈 提交于 2019-11-30 21:45:05

问题


Can I use the css calc() function when setting positions in JavaScript?

ePopup.style.top = "calc(100px - 1.5em)";

回答1:


Yes, calc() will work when setting styles in javascript.

Working Example:

var innerDiv = document.getElementsByClassName('inner-div')[0];

function growInnerDiv() {
    innerDiv.style.width = 'calc(100% + 224px)';
}

innerDiv.addEventListener('click', growInnerDiv, false);
.outer-div {
    display: inline-block;
    width: 200px;
    height: 100px;
    padding: 12px;
    border: 1px solid rgb(255,0,0);
    background-color: rgb(255,255,0);
}

.inner-div {
    width: 100px;
    height: 100px;
    color: rgb(255, 255, 255);
    font-weight: bold;
    text-align: center;
    line-height: 100px;
    font-family: arial, helvetica, sans-serif;
    background-color: rgb(255,0,0);
    cursor: pointer;
    transition: all 0.5s linear;
}
<div class="outer-div">
    <div class="inner-div">Click Me</div>
<div>


来源:https://stackoverflow.com/questions/40871127/can-i-use-css-calc-within-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!