如何在移動裝置上使自定義捲軸箭頭生效?


您可能已經注意到,一些網站具有獨特的捲軸,賦予它們獨特的感覺和外觀,因為自定義捲軸正變得越來越普遍。自定義捲軸可以通過幾種不同的方式實現。本文將使用最簡單的方法,即 CSS。

我們在應用程式中使用 CSS 來增強網頁的視覺吸引力。使用 CSS,我們現在可以更改捲軸的外觀。讓我們看看如何在移動裝置上使自定義捲軸箭頭生效。

在移動裝置上使自定義捲軸箭頭生效

過去,可以使用非標準 CSS 屬性(如 `scrollbar-base-color`)修改網站上的捲軸,您可以將其應用於滾動元素(如 `

`),並實現非常酷炫的效果。IE 放棄了這種方法。

如今,自定義捲軸再次可用,儘管這次使用了 WebKit。這些屬性現在使用“Shadow DOM”並且帶有廠商字首(例如:`-webkit-scrollbar`)。這已經存在一段時間了。

為了更深入地瞭解如何在移動裝置上使自定義捲軸箭頭生效,讓我們來看以下示例。

示例

在下面的示例中,我們使用 `webkit-scrollbar` 使捲軸在移動裝置上生效,並向捲軸應用 CSS。

<!DOCTYPE html>
<html>
   <body>
      <style>
         div{
            max-width:400px;
            max-height:250px;
            overflow-y: scroll;
            overflow-x: hidden;
         }
         div::-webkit-scrollbar {
            width: 0.5em;
         }
         div::-webkit-scrollbar-track {
            -webkit-box-shadow: inset 0 0 2px rgba(1,1,1,0.4);
         }
         div::-webkit-scrollbar-thumb {
            background-color: #D5F5E3;
            outline: 1px solid #FBFCFC;
         }
      </style>
      <div id="tutorial">
         <img src="https://tutorialspoint.tw/about/images/about-mini-logo.jpg">
         Tutorials Point originated from the idea that there exists a class of readers
         who respond better to online content and prefer to learn new skills at their
         own pace from the comforts of their drawing rooms.The journey commenced with
         a single tutorial on HTML in 2006 and elated by the response it generated, we
         worked our way to adding fresh tutorials to our repository which now proudly
         flaunts a wealth of tutorials and allied articles on topics ranging from programming
         languages to web designing to academics and much more.
      </div>
   </body>
</html>

當指令碼執行時,它將生成一個輸出,其中包含影像、一些文字和網頁上的可滾動顯示。

示例

考慮以下示例,我們使用 `webkit-scrollable` 使內容在網頁上以及箭頭一起滾動。

<!DOCTYPE html>
<html>
   <body>
      <style>
         .visible-scrollbar,
         .mostly-customized-scrollbar {
            display: block;
            width: 300px;
            overflow: auto;
            height: 150px;
         }
         .invisible-scrollbar::-webkit-scrollbar {
            display: none;
         }
         .mostly-customized-scrollbar::-webkit-scrollbar {
            width: 300px;
            height: 8px;
            background-color:#7D3C98 ;
         }
         .mostly-customized-scrollbar::-webkit-scrollbar-thumb {
            outline: 1px solid #FBFCFC;
         }
      </style>
      <div class="visible-scrollbar">
         Mahendra Singh Dhoni born 7 July 1981, commonly known as MS Dhoni,
         is a former Indian cricketer and captain of the Indian national team
         in limited-overs formats from 2007 to 2017, and in Test cricket from
         2008 to 2014. He is also the current captain of Chennai Super Kings in
         the Indian Premier League. Under his captaincy, India won the 2007 ICC
         World Twenty20, the 2011 Cricket World Cup, and the 2013 ICC Champions
         Trophy, the most by any captain. He also led India to victory in the 2010
         and 2016 Asia Cup.
      </div>
   </body>
</html>

執行上述指令碼後,將彈出輸出視窗,顯示文字以及在網頁上顯示的可滾動箭頭。

示例

執行以下程式碼,觀察我們將如何使用 `webkit-scrollable` 建立自定義可滾動區域。

<!DOCTYPE html>
<html>
   <body>
      <style>
         body {
            font-size: 15pt;
         }
         ::-webkit-scrollbar {
            width: 14px;
            border: 1px solid blue;
         }
         ::-webkit-scrollbar-button:single-button {
            background-color: fuchsia;
            height: 10px;
            width: 10px;
         }
         ::-webkit-scrollbar-thumb {
            background: maroon;
         }
         ::-webkit-scrollbar-track {
            background: silver;
         }
         ::-webkit-resizer {
            background: olive;
         }
      </style>
      <center>
         <img src="https://upload.wikimedia.org/wikipedia/commons/3/3a/Ducatilogol.png">
         <p>Ducati is a group of companies, best known for manufacturing motorcycles
         and headquartered in Borgo Panigale, Bologna, Italy. The group is owned by
         German automotive manufacturer Audi through its Italian subsidiary
         Lamborghini, which is in turn owned by the Volkswagen Group.</p>
         <br>
         <p>In the 1930s and 1940s, Ducati manufactured radios, cameras, and
         electrical products such as razors. Ducati also made a marine binocular called the BIMAR for the Kriegsmarine during World War II, some of which
         were sold on the civilian market after the war.The Ducati Sogno was
         a half-frame Leica-like camera which is now a collector's item.</p>
      </center>
   </body>
</html>

當指令碼執行時,它將生成一個輸出,其中包含文字、影像以及應用了 CSS 的自定義可滾動區域,這些都顯示在網頁上。

更新於:2023年4月20日

2K+ 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.