导航栏链接悬停效果

huangapple go评论63阅读模式
英文:

Navbar link hover effect

问题

我一直在很多网站上看到这种效果,想知道他们是如何做到的

示例:https://app.uniswap.org/#/?intro=true

当你悬停在导航栏链接上时,它看起来像一个按钮,而不是一个不同颜色的文本链接。我该怎么做?还使用bootstrap v5.3.0-alpha1,如果有任何区别的话

我有一个导航栏的例子:

```javascript
// ...(你提供的 JavaScript 代码)
<!-- ...(你提供的 HTML 代码) -->
英文:

I keep seeing this effect used on a lot of websites and am wondering how they're doing it

Example: https://app.uniswap.org/#/?intro=true

When you hover over one of the navbar links, it looks like a button instead of a different colored text link. How would I do this? Also using bootstrap v5.3.0-alpha1 if that makes any difference

I have this navbar as an example:

<!-- begin snippet: js hide: false console: true babel: true -->

<!-- language: lang-js -->

import &#39;./App.css&#39;;
import React, { useState, useEffect } from &quot;react&quot;;

const App = () =&gt; {
	return (
		&lt;div className=&quot;App&quot;&gt;
			&lt;nav className=&quot;navbar navbar-expand-sm&quot;&gt;
				&lt;div className=&quot;container-fluid&quot;&gt;
					&lt;button className=&quot;navbar-toggler&quot; type=&quot;button&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;#navbarTogglerDemo01&quot; aria-controls=&quot;navbarTogglerDemo01&quot; aria-expanded=&quot;false&quot; aria-label=&quot;Toggle navigation&quot;&gt;
						&lt;span className=&quot;navbar-toggler-icon&quot;&gt;&lt;/span&gt;
					&lt;/button&gt;
					
					&lt;a className=&quot;navbar-brand d-none d-md-block&quot; href=&#39;/&#39;&gt;
						&lt;img src={&quot;./logo.png&quot;} style={{ height: &#39;50px&#39;, width: &#39;50px&#39;}} alt=&#39;&#39; /&gt;
					&lt;/a&gt;

					&lt;div className=&quot;collapse navbar-collapse text-center justify-content-center&quot; id=&quot;navbarTogglerDemo01&quot;&gt;
						&lt;div className=&quot;navbar-nav&quot;&gt;
							&lt;a className=&quot;nav-link active&quot; aria-current=&quot;page&quot; href=&quot;/#&quot;&gt;Home&lt;/a&gt;
							&lt;a className=&quot;nav-link&quot; href=&quot;/#&quot;&gt;about_1&lt;/a&gt;
							&lt;a className=&quot;nav-link&quot; href=&quot;/#&quot;&gt;about_2&lt;/a&gt;
							&lt;a className=&quot;nav-link disabled&quot; href=&#39;/&#39;&gt;Disabled&lt;/a&gt;
						&lt;/div&gt;
					&lt;/div&gt;

					&lt;div className=&quot;btn-group&quot;&gt;
						&lt;WagmiConfig client={wagmiClient}&gt;
							&lt;RainbowKitProvider 
								chains={chains} 
								initialChain={mainnet}
								theme={lightTheme({
									borderRadius: &#39;small&#39;
								})}
							&gt;
								&lt;ConnectButton accountStatus=&#39;address&#39; chainStatus=&#39;none&#39; showBalance={false} /&gt;
							&lt;/RainbowKitProvider&gt;
						&lt;/WagmiConfig&gt;

						&lt;div className=&quot;dropdown&quot;&gt;
							&lt;button className=&quot;btn dropdown-toggle&quot; type=&quot;button&quot; data-bs-toggle=&quot;dropdown&quot; aria-expanded=&quot;false&quot; data-bs-auto-close=&quot;outside&quot;&gt;
								&lt;i className=&quot;bi bi-three-dots&quot;&gt;&lt;/i&gt;
							&lt;/button&gt;
							&lt;ul className=&quot;dropdown-menu dropdown-menu-end&quot; style={{ width: &#39;30vw&#39;}}&gt;
								&lt;li&gt;&lt;a className=&quot;dropdown-item&quot; href=&quot;#&quot;&gt;Action&lt;/a&gt;&lt;/li&gt;
								&lt;li&gt;&lt;a className=&quot;dropdown-item&quot; href=&quot;#&quot;&gt;Another action&lt;/a&gt;&lt;/li&gt;
								&lt;li&gt;&lt;a className=&quot;dropdown-item&quot; href=&quot;#&quot;&gt;Something else here&lt;/a&gt;&lt;/li&gt;
								{/* &lt;li&gt;&lt;i className={darkMode ? &#39;fas fa-sun&#39; : &#39;fas fa-moon&#39;}&gt;&lt;/i&gt;&lt;/li&gt; */}
							&lt;/ul&gt;
						&lt;/div&gt;
					&lt;/div&gt;
				&lt;/div&gt;
			&lt;/nav&gt;
	);
};

export default App;

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js&quot;&gt;&lt;/script&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
	&lt;head&gt;
		&lt;meta charset=&quot;utf-8&quot; /&gt;
		&lt;link rel=&quot;icon&quot; href=&quot;%PUBLIC_URL%/favicon.ico&quot; /&gt;
		&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot; /&gt;
		&lt;meta name=&quot;theme-color&quot; content=&quot;#000000&quot; /&gt;
		&lt;meta
			name=&quot;description&quot;
			content=&quot;Web site created using create-react-app&quot;
		/&gt;
		&lt;link rel=&quot;apple-touch-icon&quot; href=&quot;%PUBLIC_URL%/logo192.png&quot; /&gt;
		&lt;!--
			manifest.json provides metadata used when your web app is installed on a
			user&#39;s mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
		--&gt;
		&lt;link rel=&quot;manifest&quot; href=&quot;%PUBLIC_URL%/manifest.json&quot; /&gt;
		&lt;!--
			Notice the use of %PUBLIC_URL% in the tags above.
			It will be replaced with the URL of the `public` folder during the build.
			Only files inside the `public` folder can be referenced from the HTML.

			Unlike &quot;/favicon.ico&quot; or &quot;favicon.ico&quot;, &quot;%PUBLIC_URL%/favicon.ico&quot; will
			work correctly both with client-side routing and a non-root public URL.
			Learn how to configure a non-root public URL by running `npm run build`.
		--&gt;
		&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD&quot; crossorigin=&quot;anonymous&quot;&gt;
		&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.3/font/bootstrap-icons.css&quot;&gt;
		&lt;title&gt;React App&lt;/title&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;noscript&gt;You need to enable JavaScript to run this app.&lt;/noscript&gt;
		&lt;div id=&quot;root&quot;&gt;&lt;/div&gt;

		&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js&quot; integrity=&quot;sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
	&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 2

这只是在"hover"状态下添加了background-color。还要在默认情况下为链接添加一些padding

ul {
  display: flex;
  list-style: none;
}

li {
  margin-right: 15px;
}

a {
  padding: 8px 15px;
  color: #000;
  text-decoration: none;
}

li:hover a {
  background: #e1e1e1;
  border-radius: 5px;
}
<nav>
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
  </ul>
</nav>
英文:

It's just adding a background-color on hover. Also, add some padding around the links by default.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

ul {
display: flex;
list-style: none
}

li {
  margin-right: 15px;
}

a {
padding: 8px 15px;
color: #000;
text-decoration: none;
}

li:hover a {
background: #e1e1e1;
border-radius: 5px;

}

<!-- language: lang-html -->

&lt;nav&gt;
  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;About&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/nav&gt;

<!-- end snippet -->

答案2

得分: 0

nav ul {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
}

nav li {
    margin-right: 20px;
}

nav a {
    text-decoration: none;
    color: #000;
    transition: color 0.3s ease;
}

nav a:hover {
    color: #fff;
    background-color: #000;
}

在上面的CSS代码中,我们首先定义了导航栏的基本样式,包括ul元素的flexbox布局以及li元素的一些边距和填充调整。

然后,我们用text-decoration: none样式为a元素添加了文本装饰,并将默认颜色设置为黑色。我们还添加了一个过渡属性,以在链接被悬停时动画变色。

最后,我们定义了a元素的:hover样式。当链接被悬停时,我们将颜色更改为白色,并添加背景颜色为黑色,以与文本形成对比。这为导航栏链接创建了简单但有效的悬停效果。

英文:
nav ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
}

nav li {
margin-right: 20px;
}

nav a {
text-decoration: none;
color: #000;
transition: color 0.3s ease;
}

nav a:hover {
color: #fff;
background-color: #000;
}

In the CSS code above, we first define the basic styles for the navbar,
including a flexbox layout for the ul element and some margin and padding
adjustments for the li elements.

Then, we style the a elements with text-decoration: none and a default
color of black. We also add a transition property to animate the color
change when the link is hovered over.

Finally, we define the :hover styles for the a elements. When a link is
hovered over, we change the color to white and add a background-color of
black to create a contrast with the text. This creates a simple but
effective hover effect for the navbar links.`

huangapple
  • 本文由 发表于 2023年2月16日 13:17:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468110.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定