use*_*454 4 html css mouse scroll
我想设计一个横向页面.当我使用鼠标滚轮时,它只是垂直滚动内容.如何通过鼠标滚轮使用水平滚动?CSS是否支持此属性?
例如,像这样的事情:
mouse-wheel:horizontal;
Run Code Online (Sandbox Code Playgroud)
我不想要jQuery解决方案.
小智 20
var item = document.getElementById("MAIN");
window.addEventListener("wheel", function (e) {
if (e.deltaY > 0) item.scrollLeft += 100;
else item.scrollLeft -= 100;
});
Run Code Online (Sandbox Code Playgroud)
"MAIN"你的容器在哪里
mic*_*ing 17
CSS不支持操纵鼠标或键盘输入; 输入控件只能通过JavaScript操作.
css中没有这样的样式
div {
scroll-direction: horizontal;
}
Run Code Online (Sandbox Code Playgroud)
这可能非常方便。但是,您有办法做到这一点,
您也可以创建自己的方法。
轻松使用 JavaScript
const container = document.getElementById("container");
// where "container" is the id of the container
container.addEventListener("wheel", function (e) {
if (e.deltaY > 0) {
container.scrollLeft += 100;
e.preventDefault();
// prevenDefault() will help avoid worrisome
// inclusion of vertical scroll
}
else {
container.scrollLeft -= 100;
e.preventDefault();
}
});
// That will work perfectly
Run Code Online (Sandbox Code Playgroud)
或者,如果您喜欢使用 CSS,这将创建容器、旋转它、旋转项目并滚动。
步骤:
container项目并添加 css:<!------ HTML ------>
<div class="container">
<div>item 1</div>
<div>item 2</div>
<div>item 3</div>
<div>item 4</div>
<div>item 5</div>
<div>item 6</div>
<div>item 7</div>
<div>item 8</div>
</div>
Run Code Online (Sandbox Code Playgroud)
/** CSS **/
.container {
width: 100px;
height: 300px;
overflow-y: auto;
overflow-x: hidden;
transform: rotate(-90deg);
transform-origin: right top;
transform:rotate(-90deg) translateY(-100px);
}
.container > div {
width: 100px;
height: 100px;
transform: rotate(90deg);
transform-origin: right top;
}
Run Code Online (Sandbox Code Playgroud)
这对你有帮助吗?
将容器旋转–90度,然后将其子元素旋转90度。
.container {
width: 180px;
height: 600px;
overflow-y: scroll;
transform: rotate(-90deg);
}
.content {
width: 140px;
height: 140px;
margin: 10px;
background-color: #a8a8df;
transform: rotate(90deg);
}Run Code Online (Sandbox Code Playgroud)
<div class="container">
<div class="content">Content 1</div>
<div class="content">Content 2</div>
<div class="content">Content 3</div>
<div class="content">Content 4</div>
<div class="content">Content 5</div>
<div class="content">Content 6</div>
<div class="content">Content 7</div>
</div>Run Code Online (Sandbox Code Playgroud)
参见Pieter Biesemans在CSS Tricks上的文章。他还列出了浏览器兼容性。
| 归档时间: |
|
| 查看次数: |
19058 次 |
| 最近记录: |