組件分頁
The theme for week #17 of the Weekly Coding Challenge is:
每周編碼挑戰第17周的主題是:
分頁 (Pagination)
A Pagination Component is used on websites where you have more content available than you want to display at one time to the user so you'd split it on multiple pages. By separating the content on different pages you are also saving a lot of bandwidth for the user because it won't be required to download all the information at once.
分頁組件用于網站上,您所擁有的內容比一次要向用戶顯示的內容要多,因此您可以將其拆分為多個頁面。 通過分隔不同頁面上的內容,您還為用戶節省了大量帶寬,因為不需要一次下載所有信息。
Some examples where you'd have a pagination: a blog with multiple pages, an online store with multiple products, etc.
您可以進行分頁的一些示例 :具有多個頁面的博客,具有多種產品的在線商店等。
In this article we're going to build this Pagination Component:
在本文中,我們將構建此分頁組件 :
Note: the pagination is not functional, it's just for demo purposes (the visual). As an extra challenge, you can link this on a real website.
注意 :分頁功能不起作用,僅用于演示目的(視覺效果)。 作為額外的挑戰,您可以在一個真實的網站上鏈接它。
HTML (The HTML)
For the HTML structure we're going to use an ul
as a wrapper with multiple li
s. Each li
will have an a
tag within it because it's clickable (and semantic) and it'll send the user to the required page (if needed).
對于HTML結構,我們將使用ul
作為帶有多個li
的包裝器。 每個li
都將在其中包含a
標簽,因為它是可單擊的(和語義),并且會將用戶定向到所需的頁面(如果需要)。
We're also using FontAwesome for the icons (left, right and the dots icons).
我們還將FontAwesome用于圖標(左側,右側和圓點圖標)。
<ul class="pagination"><li><a href="#"><i class="fas fa-chevron-left"></i></a></li><li><a href="#"><i class="fas fa-ellipsis-h"></i></a></li><li><a href="#">2</a></li><li class="active"><a href="#">3</a></li><li><a href="#">4</a></li><li><a href="#">5</a></li><li><a href="#"><i class="fas fa-ellipsis-h"></i></a></li><li><a href="#"><i class="fas fa-chevron-right"></i></a></li>
</ul>
As you can see I also added an .active
class to one of the li
s - this is just to highlight the page we are on.
正如你可以看到我還增加了.active
類的一個li
秒-這僅僅是重點突出一頁,我們都在。
CSS (The CSS)
I'm going to paste the CSS and we'll discuss the important pieces afterwards.
我將粘貼CSS,然后我們將討論重要的部分。
.pagination {border: 2px solid #aaa;border-radius: 4px;display: flex;list-style-type: none;overflow: hidden;padding: 0;
}.pagination li {background-color: #fff;
}.pagination li:hover,
.pagination li.active {background-color: #aaa;
}.pagination li a {color: #555;display: block;font-weight: bold;padding: 10px 15px;text-decoration: none;
}.pagination li:hover a,
.pagination li.active a {color: #fff;
}
Things to note:
注意事項:
The
ul
/.pagination
it's a flex container - this is because it's much easier to position theli
s within it by using flexbox (and who doesn't like flexbox? ?)ul
/.pagination
是一個flex容器-這是因為使用flexbox將li
放置在其中很容易(而且誰不喜歡flexbox ??)Because we have a little bit of a
border-radius
on theul
we need to addoverflow: hidden
because otherwise theli
's corner will be visible on top of the border of theul
(remove the overflow and you'll see what I mean)因為我們在
ul
上有一個小border-radius
,所以我們需要添加overflow: hidden
因為否則,li
的角將在ul
的邊界上可見(去除溢出,您將看到我意思)We have the same styling on
li:hover
as onli.active
, but you can differentiate between these to states if you want我們在
li:hover
上的樣式與在li.active
上的樣式相同,但是您可以根據需要在狀態之間進行區分
Other than that, I believe it's pretty straightforward, but if you have any questions please let me know. ?
除此之外,我相信這非常簡單,但是如果您有任何疑問,請告訴我。 ?
結論 (Conclusion)
There are other variants of paginations out there on the web. Find one you like and convert it to code.
網絡上還有其他分頁方式。 找到您喜歡的一個并將其轉換為代碼。
Make sure you share with me what you've built!
確保您與我分享您的作品!
Happy Coding! ?
編碼愉快! ?
翻譯自: https://www.freecodecamp.org/news/how-to-create-a-pagination-component/
組件分頁