当在包含各种元素的div
上使用transform: scale(...)
时,除了<select>
标记的“下拉”部分之外,所有内容都会按预期缩放:
function test() {
alert("test");
}
let contentDiv = document.getElementById("contentDiv");
let scaleSlider = document.getElementById("scaleSlider");
console.log(contentDiv);
console.log(scaleSlider);
scaleSlider.oninput = function() {
contentDiv.style = "transform: scale(" + this.value + ")";
};
<div>
<input type="range" min="0" max="1" value="1" step="0.1"
class="slider" id="scaleSlider" style="direction: rtl">
<div id="contentDiv">
<button onclick="test()">test</button>
<div style="background:green; width:100px; height:100px"></div>
<label>some text</label>
<br />
<input type="text" name="textInput"></input>
<br />
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<br />
<img src="https://via.placeholder.com/150"></img>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
<progress value="22" max="100"></progress>
<br />
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>
<br />
<textarea rows="4" cols="50">
At w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.
</textarea>
</div>
</div>
如果将scale
更改为rotate
,则会得到类似的结果。似乎<select>
的下拉部分根本不受CSS2D转换的影响。
为什么会这样呢?有什么办法可以绕过它吗?我希望使用CSS 2D变换作为“逐元素”缩放的一种健壮方式,但像这样的边缘情况让我对这个想法保持警惕。
发布于 2019-01-07 08:18:09
下拉选项是由您的操作系统呈现的,您不能将CSS属性应用于它们(一些浏览器支持一些微不足道的属性,如font-weight
)。您可以考虑使用类似于选择下拉列表的第三方库,并对其应用CSS样式。
查看Select2,它是自定义选择的一个很好的例子。
https://stackoverflow.com/questions/54067115
复制相似问题