css - 如何使用 CSS 以像素为单位缩放元素?

标签 css

元素可以用 transform: scale(sx) 缩放,其中 sx是(无单位)比例因子。有没有办法将元素缩小一定数量的像素(不知道它的宽度)。

编辑

我想在按下按钮时调整不同大小的元素的大小,例如按钮。但是,应用相同的比例因子,比如 scale(.95) , 在所有按钮上都会导致较大(较小)的按钮被缩放太多(小)。所以,我想做的是在绝对按下时调整所有元素的大小,即按一定数量的像素或 em 左右。

最佳答案

这就是使用 vanilla JavaScript 缩放宽度的方法

const reduction=10;//in pixels
let button = document.querySelector("#myButton");
button.addEventListener("click",(event)=>{
  let style=window.getComputedStyle(button);
  let realWidth=parseInt(style.width);
  console.log(realWidth);
  let newWidth=realWidth-reduction;//some redundancy here
  button.style.width=`${newWidth}px`;//this changes the actual width property. With jQuery it would be even easier.
},false);
#myButton{
width:300px;
height:50px;
}
<button id="myButton">Click here to resize</button>

关于css - 如何使用 CSS 以像素为单位缩放元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52140628/

相关文章:

javascript - 在有滚动条的侧边栏外显示下拉菜单,但仍与 anchor 处于相同位置

css - 将 table 设置为 70% 但将 td 设置为 50%?

html - 自动设置添加到网页的新信息 block 的样式

html - 显示 : none; not working twice

css - 使一个 Div 的高度填充剩余空间,但不要超过另一个 Div

javascript - 拉伸(stretch)高度/宽度以适应 <Div> 标签中的屏幕

html - CSS3 旋转转换导致间歇性鼠标点击检测

jquery - 使用 jQuery 实时更新文本

php - WordPress Customizer API (Kirki) - Repeater Image

javascript - 如何使淡入淡出过渡更快 css3?