最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Is it possible to use :hover for -webkit-scrollbar? - Stack Overflow

programmeradmin6浏览0评论

I would like to show the scrollbar when mouse cursor is on.

I try like this using :hover, but in vain.

Is there any solution?

.myBox{
width:100px;
height:50px;
border:1px solid black;
overflow-y:scroll;
overflow-x:none;
}

.myBox::-webkit-scrollbar{
  display: none;
}
.myBox:hover::-webkit-scrollbar{
  display: block;
}
<div class="myBox">
test<br>
test1<br>
test2<br>
test3<br>
test4<br>
<div>

I would like to show the scrollbar when mouse cursor is on.

I try like this using :hover, but in vain.

Is there any solution?

.myBox{
width:100px;
height:50px;
border:1px solid black;
overflow-y:scroll;
overflow-x:none;
}

.myBox::-webkit-scrollbar{
  display: none;
}
.myBox:hover::-webkit-scrollbar{
  display: block;
}
<div class="myBox">
test<br>
test1<br>
test2<br>
test3<br>
test4<br>
<div>

Share Improve this question asked Mar 21 at 3:56 whitebearwhitebear 12.5k29 gold badges149 silver badges299 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

you can try like this

.myBox {
  width: 100px;
  height: 50px;
  border: 1px solid black;
  overflow-y: auto;  /* Changed from scroll to auto */
  overflow-x: hidden; /* Changed from none to hidden */
}

/* Hide scrollbar by default */
.myBox::-webkit-scrollbar {
  width: 8px; /* Set a width instead of display:none */
  background: transparent;
}

.myBox::-webkit-scrollbar-thumb {
  background-color: transparent; /* Make the thumb transparent by default */
}

/* Show scrollbar on hover */
.myBox:hover::-webkit-scrollbar-thumb {
  background-color: #888; /* Show a visible color on hover */
  border-radius: 4px;
}

/* Optional: style the track on hover */
.myBox:hover::-webkit-scrollbar-track {
  background: #f1f1f1;
}
<div class="myBox">
test<br>
test1<br>
test2<br>
test3<br>
test4<br>
<div>

发布评论

评论列表(0)

  1. 暂无评论