gpt4 book ai didi

html - 如何仅使用 CSS 使内容进入滚动条?

转载 作者:行者123 更新时间:2023-12-03 17:01:23 26 4
gpt4 key购买 nike

我知道有各种用于自定义滚动的 JS 库,但我相信对于现代浏览器,最好采用更一致和可预测的 native 行为。我想我会有 nice Chrome/Edge 中的滚动条(闪烁),acceptable在 FF 中使用他们自己简单的颜色/大小自定义,我不会关心其他浏览器。

enter image description here
我现在面临的唯一问题是 - 我想要 li滚动条下的元素。我试图通过 transform: translateX(15px) 移动它下面的内容/margin-right: -15px/right: -15px/overflow: overlay没有任何帮助( overflow:overlay<body/> 做这项工作,它对内部容器没有帮助 )。

在没有 JS 的情况下实现所需行为的任​​何技巧?

*::-webkit-scrollbar-track {
background-color: transparent;
}

*::-webkit-scrollbar {
background-color: transparent;
transition: .3s;
}

*:hover::-webkit-scrollbar {
width: 15px !important;
}

*::-webkit-scrollbar-thumb {
border-radius: 10px;
border: 2px solid #444;
}

ul {
margin: 0;
padding: 0;
height: 100vh;
width: 70vw;
overflow-y: scroll;
background: linear-gradient(to top, #c6ffdd, #fbd786, #f7797d);
}

li {
cursor: pointer;
height: 40px;
transition: background .2s;
display: block;
}

li:hover {
background: rgba(255, 255, 255, 0.4);
}

ul:after {
content: "Scroll ↧";
color: white;
letter-spacing: 10px;
position: fixed;
left: 50%;
top: 50%;
font-size: 40px;
line-height: 1;
transform: translateX(-50%) translateY(-50%);
mix-blend-mode: difference;
}

body {
background: #12c2e9;
background: #c471ed20;
background: #444;
justify-content: center;
display: flex;
padding: 0;
margin: 0;
}
<ul>
<li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li>
</ul>

最佳答案

overflow: overlay做你想做的。但请注意,此功能不是 standard .此外,Edge 还将拥有另一个属性 -ms-overflow-style: -ms-autohiding-scrollbar; to hide scrollbar .还要在 Firefox 中仔细测试。
overflow Prop 应该放在body元素,因此滚动条覆盖在 ul 的顶部.

body {
padding: 0;
margin: 0;
overflow: overlay;
}

ul {
margin: 0;
padding: 0;
background: linear-gradient(to top, #c6ffdd, #fbd786, #f7797d);
}

Working sample (在 Chrome 中测试)

关于html - 如何仅使用 CSS 使内容进入滚动条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60162447/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com