【问题标题】:Is it possible for me to use CSS to have an image light up with mouseover?我是否可以使用 CSS 让鼠标悬停时点亮图像?
【发布时间】:2021-02-06 22:59:48
【问题描述】:

我正在尝试使用 CSS 和 HTML 来创建一种效果,使某人可以将鼠标悬停在我页面上的特定图像上,并且图像看起来会“亮起”,或者至少会做一些很酷的事情。在包含的 sn-p 中,我试图对 innerArticle 类中的圆形图像执行此操作。实现这一目标的有效方法是什么?

body {
  display: grid;
  grid-template-areas:
    'header header header'
    'nav article ads'
    'nav footer footer';
  grid-template-rows: 80px 1fr 70px;
  grid-template-columns: 20% 1fr 15%;
  grid-row-gap: 1px;
  grid-column-gap: 1px;
  height: 100vh;
  margin: 0;
  padding: 0;
}

a {
  color: #0ad05b;
  text-decoration: none;
}

a:hover {
  color: #e3eaee;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.2em;
  background: #21313c;
  font-family: Helvetica, Arial, sans-serif;
  color: #e3eaee;
  font-size: 14px;
}
footer,
article,
nav,
div {
  padding: 1.2em;
  background: #061621;
  font-family: Helvetica, Arial, sans-serif;
  color: #e3eaee;
  font-size: 14px;
}
textarea {
  font-family: Arial, Helvetica, sans-serif;
}
#pageHeader {
  grid-area: header;
}
#pageFooter {
  grid-area: footer;
}
#mainArticle {
  grid-area: article;
  align-content: flex-start;
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
  justify-content: flex-start;
}
#mainArticleShip {
  grid-area: article;
  align-content: flex-start;
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
  justify-content: flex-start;
}
#mainNav {
  grid-area: nav;
}
#siteAds {
  grid-area: ads;
}
/* Form Style */
#mainArticleForm {
  background: linear-gradient(50deg, rgba(6, 22, 33, 0.5), rgb(0, 0, 0)),
    url(https://cruisecrunch.s3.us-east-2.amazonaws.com/bg.jpg);
  background-size: cover;
  background-repeat: no-repeat;
  background-position: right;
}
#mainArticle {
  background: linear-gradient(15deg, rgba(6, 22, 33, 0.7), rgb(0, 0, 0)),
    url(https://cruisecrunch.s3.us-east-2.amazonaws.com/bg.jpg);
  background-size: cover;
  background-repeat: no-repeat;
  background-position: right;
}
.innerArticleSignup {
  background: transparent;
}
.innerArticleForm {
  width: 65%;
  margin: 0 auto;
  background: transparent;
}
.text {
  background: transparent;
}
.reviewForm {
  background: transparent;
}
.innerArticleForm h1 {
  color: #e3eaee;
  text-align: center;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 30px;
}
.innerArticleForm h2 {
  color: #e3eaee;
  text-align: center;
  font-size: 18px;
}
form {
  padding: 39px;
}
.form__group {
  background: #000000a1;
  display: flex;
  justify-content: space-between;
}
.form__group label {
  width: 232px;
  display: inline;
  text-align: left;
}
.form__group select {
  padding: 6px;
  float: right;
}
.form__group input {
  padding: 6px;
  font-size: 14px;
  border-radius: 4px;
  float: right;
}
.form__group textarea {
  font-size: 14px;
  float: right;
  height: auto;
  width: 100%;
  color: #767676;
}

.primary {
  padding: 8px 18px;
  margin: 22px 0px;
  font-size: 18px;
  background: #0ad05b;
  border: 2px solid #989898;
}
.innerArticle {
  height: 17vh;
  width: 17vh;
  background: transparent;
  margin-top: 1rem;
  margin-bottom: 1rem;
}
.big-user-photo {
  border-radius: 50%;
}
.userAvatar {
  height: 3rem;
  width: 3rem;
  border-radius: 50%;
  margin-right: 1rem;
}
.mainPage {
  height: 3rem;
  width: 3rem;
  border-radius: 50%;
  margin-right: 1rem;
}
.headerRight {
  grid-area: header;
  justify-self: right;
  padding: 1.2em;
  background: #21313c;
  font-family: Helvetica, Arial, sans-serif;
  color: #e3eaee;
}
.right {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  padding: 0.5em;
  background: #21313c;
  font-family: Helvetica, Arial, sans-serif;
  color: #e3eaee;
}

.left {
  background: #21313c;
  font-family: Helvetica, Arial, sans-serif;
  color: #e3eaee;
  align-content: left;
  align-items: left;
  justify-content: left;
}

.form-user-photo {
  height: 15rem;
  width: 15rem;
}

.alert {
  position: fixed;
  top: 0;
  left: 50%;
  -webkit-transform: translateX(-50%);
  transform: translateX(-50%);
  z-index: 9999;
  color: #fff;
  font-size: 1.8rem;
  font-weight: 400;
  text-align: center;
  border-bottom-left-radius: 5px;
  border-bottom-right-radius: 5px;
  padding: 1.6rem 15rem;
  -webkit-box-shadow: 0 2rem 4rem rgba(0, 0, 0, 0.25);
  box-shadow: 0 2rem 4rem rgba(0, 0, 0, 0.25);
}
.alert--success {
  background-color: #0ad05b;
}
.alert--error {
  background-color: #eb4d4b;
}
.stars-container {
  position: relative;
  display: inline-block;
  color: transparent;
}

.stars-container:before {
  position: absolute;
  top: 0;
  left: 0;
  content: '★★★★★';
  color: lightgray;
}

.stars-container:after {
  position: absolute;
  top: 0;
  left: 0;
  content: '★★★★★';
  color: gold;
  overflow: hidden;
}

.stars-0:after {
  width: 0%;
}
.stars-10:after {
  width: 10%;
}
.stars-20:after {
  width: 20%;
}
.stars-30:after {
  width: 30%;
}
.stars-40:after {
  width: 40%;
}
.stars-50:after {
  width: 50%;
}
.stars-60:after {
  width: 60%;
}
.stars-70:after {
  width: 70%;
}
.stars-80:after {
  width: 80%;
}
.stars-90:after {
  width: 90%;
}
.stars-100:after {
  width: 100;
}

.shipImage {
  width: 200px;
  height: auto;
  border-radius: 10%;
}

.logo {
  width: 100%;
  max-width: 175px;
  height: auto;
}

.otherShips {
  align-content: flex-start;
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
  justify-content: flex-start;
}
/* Stack the layout on small devices/viewports. */
@media all and (max-width: 575px) {
  body {
    grid-template-areas:
      'header'
      'article'
      'ads'
      'nav'
      'footer';
    grid-template-rows: 80px 1fr 70px 1fr 70px;
    grid-template-columns: 1fr;
  }
  .form__group {
    display: block;
  }
}
@media (max-width: 1250px) {
  .innerArticleForm,
  .reviewForm,
  form {
    padding: 0;
  }
  .innerArticleForm {
    width: 70%;
  }
}
@media (max-width: 557px) {
  .innerArticleForm {
    width: 100%;
  }
}
<!DOCTYPE html>

<head>
    
</head>

<body>
    <header id="pageHeader">
        <div class="left"><a href="/"><img class="logo"
                    src="https://cruisecrunch.s3.us-east-2.amazonaws.com/logo.png"></a></div>
        <div class="right">
            <p><a href="/signup">Sign up</a> | <a href="/login">Login</a></p>
        </div>
    </header>
    <article id="mainArticle">
        <div class="text">
            <h2>All Cruise Ships</h2>
            <p>Welcome to CruiseCrunch - the ultimate destination for cruise research. Enjoy free access to user reviews
                and ratings for cruise ships across the industry. If you have cruised before, please <a
                    href="/addreview">leave a review</a> of your trip for others. </p>
        </div>
        <div class="innerArticle"><a href="/ship/disney-dream"><img class="mainPage" src="https://www.goport.com/blog/wp-content/uploads/2013/10/ship-photo-shopped.jpg"
                    alt="Disney Dream"></a><br>Disney Dream<br><br><span
                class="stars-container stars-20">★★★★★</span><br><br><a href="/ship/disney-dream">See Reviews (1)</a>
        </div>
    </article>
    <nav id="mainNav">Navigation<div class="nav">
            <div class="ul">
                <div class="li"><a href="/"><i class="fa fa-ship"></i> All Ships</a></div>
                <div class="li"><a href="/addreview"><i class="fa fa-plus"></i> Add Review</a></div>
                <div class="li"><a href="/topships"><i class="fa fa-trophy"></i> Top Rated Ships</a></div>
                <div class="li"> <a href="/bestforkids"><i class="fa fa-child"></i> Best For Kids</a></div>
                <div class="li"> <a href="/bestvalue"><i class="fa fa-dollar"></i> Best Value</a></div>
                <div class="li"><a href="/cabins"><i class="fa fa-bed"></i> Best Cabins</a></div>
                <div class="li"><a href="/bestdining"><i class="fa fa-beer"></i> Best Dining</a></div>
                <div class="li"><a href="/entertainment"><i class="fa fa-film"></i> Best Entertainment</a></div>
                <div class="li"><a href="/worstships"><i class="fa fa-thumbs-down"></i> Lowest Rated Ships</a></div>
                <div class="li"><a href="/recentreviews"><i class="fa fa-rss-square"></i> Most Recent Reviews</a></div>
                <div class="li"><a href="/newships"><i class="fa fa-anchor"></i> New Ships</a></div>
                <div class="li"><br></div>
            </div>
        </div>
    </nav>
    <div id="siteAds">Over 6 cruise ship reviews!</div>
    <footer id="pageFooter">
        <p class="footer">&#169; 2020, CruiseCrunch</p>
    </footer>
</body>

【问题讨论】:

标签: html css image


【解决方案1】:

您可以使用filter 并重置brightness

img.mainPage {
  filter: brightness(50%);
  transition:.1s cubic-bezier(.20,-4,.80,4)
;
}

img.mainPage:hover {
  filter: brightness(100%);
}

body {
  display: grid;
  grid-template-areas: 'header header header' 'nav article ads' 'nav footer footer';
  grid-template-rows: 80px 1fr 70px;
  grid-template-columns: 20% 1fr 15%;
  grid-row-gap: 1px;
  grid-column-gap: 1px;
  height: 100vh;
  margin: 0;
  padding: 0;
}

a {
  color: #0ad05b;
  text-decoration: none;
}

a:hover {
  color: #e3eaee;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.2em;
  background: #21313c;
  font-family: Helvetica, Arial, sans-serif;
  color: #e3eaee;
  font-size: 14px;
}

footer,
article,
nav,
div {
  padding: 1.2em;
  background: #061621;
  font-family: Helvetica, Arial, sans-serif;
  color: #e3eaee;
  font-size: 14px;
}

textarea {
  font-family: Arial, Helvetica, sans-serif;
}

#pageHeader {
  grid-area: header;
}

#pageFooter {
  grid-area: footer;
}

#mainArticle {
  grid-area: article;
  align-content: flex-start;
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
  justify-content: flex-start;
}

#mainArticleShip {
  grid-area: article;
  align-content: flex-start;
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
  justify-content: flex-start;
}

#mainNav {
  grid-area: nav;
}

#siteAds {
  grid-area: ads;
}


/* Form Style */

#mainArticleForm {
  background: linear-gradient(50deg, rgba(6, 22, 33, 0.5), rgb(0, 0, 0)), url(https://cruisecrunch.s3.us-east-2.amazonaws.com/bg.jpg);
  background-size: cover;
  background-repeat: no-repeat;
  background-position: right;
}

#mainArticle {
  background: linear-gradient(15deg, rgba(6, 22, 33, 0.7), rgb(0, 0, 0)), url(https://cruisecrunch.s3.us-east-2.amazonaws.com/bg.jpg);
  background-size: cover;
  background-repeat: no-repeat;
  background-position: right;
}

.innerArticleSignup {
  background: transparent;
}

.innerArticleForm {
  width: 65%;
  margin: 0 auto;
  background: transparent;
}

.text {
  background: transparent;
}

.reviewForm {
  background: transparent;
}

.innerArticleForm h1 {
  color: #e3eaee;
  text-align: center;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 30px;
}

.innerArticleForm h2 {
  color: #e3eaee;
  text-align: center;
  font-size: 18px;
}

form {
  padding: 39px;
}

.form__group {
  background: #000000a1;
  display: flex;
  justify-content: space-between;
}

.form__group label {
  width: 232px;
  display: inline;
  text-align: left;
}

.form__group select {
  padding: 6px;
  float: right;
}

.form__group input {
  padding: 6px;
  font-size: 14px;
  border-radius: 4px;
  float: right;
}

.form__group textarea {
  font-size: 14px;
  float: right;
  height: auto;
  width: 100%;
  color: #767676;
}

.primary {
  padding: 8px 18px;
  margin: 22px 0px;
  font-size: 18px;
  background: #0ad05b;
  border: 2px solid #989898;
}

.innerArticle {
  height: 17vh;
  width: 17vh;
  background: transparent;
  margin-top: 1rem;
  margin-bottom: 1rem;
}

.big-user-photo {
  border-radius: 50%;
}

.userAvatar {
  height: 3rem;
  width: 3rem;
  border-radius: 50%;
  margin-right: 1rem;
}

.mainPage {
  height: 3rem;
  width: 3rem;
  border-radius: 50%;
  margin-right: 1rem;
}

.headerRight {
  grid-area: header;
  justify-self: right;
  padding: 1.2em;
  background: #21313c;
  font-family: Helvetica, Arial, sans-serif;
  color: #e3eaee;
}

.right {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  padding: 0.5em;
  background: #21313c;
  font-family: Helvetica, Arial, sans-serif;
  color: #e3eaee;
}

.left {
  background: #21313c;
  font-family: Helvetica, Arial, sans-serif;
  color: #e3eaee;
  align-content: left;
  align-items: left;
  justify-content: left;
}

.form-user-photo {
  height: 15rem;
  width: 15rem;
}

.alert {
  position: fixed;
  top: 0;
  left: 50%;
  -webkit-transform: translateX(-50%);
  transform: translateX(-50%);
  z-index: 9999;
  color: #fff;
  font-size: 1.8rem;
  font-weight: 400;
  text-align: center;
  border-bottom-left-radius: 5px;
  border-bottom-right-radius: 5px;
  padding: 1.6rem 15rem;
  -webkit-box-shadow: 0 2rem 4rem rgba(0, 0, 0, 0.25);
  box-shadow: 0 2rem 4rem rgba(0, 0, 0, 0.25);
}

.alert--success {
  background-color: #0ad05b;
}

.alert--error {
  background-color: #eb4d4b;
}

.stars-container {
  position: relative;
  display: inline-block;
  color: transparent;
}

.stars-container:before {
  position: absolute;
  top: 0;
  left: 0;
  content: '★★★★★';
  color: lightgray;
}

.stars-container:after {
  position: absolute;
  top: 0;
  left: 0;
  content: '★★★★★';
  color: gold;
  overflow: hidden;
}

.stars-0:after {
  width: 0%;
}

.stars-10:after {
  width: 10%;
}

.stars-20:after {
  width: 20%;
}

.stars-30:after {
  width: 30%;
}

.stars-40:after {
  width: 40%;
}

.stars-50:after {
  width: 50%;
}

.stars-60:after {
  width: 60%;
}

.stars-70:after {
  width: 70%;
}

.stars-80:after {
  width: 80%;
}

.stars-90:after {
  width: 90%;
}

.stars-100:after {
  width: 100;
}

.shipImage {
  width: 200px;
  height: auto;
  border-radius: 10%;
}

.logo {
  width: 100%;
  max-width: 175px;
  height: auto;
}

.otherShips {
  align-content: flex-start;
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
  justify-content: flex-start;
}


/* Stack the layout on small devices/viewports. */

@media all and (max-width: 575px) {
  body {
    grid-template-areas: 'header' 'article' 'ads' 'nav' 'footer';
    grid-template-rows: 80px 1fr 70px 1fr 70px;
    grid-template-columns: 1fr;
  }
  .form__group {
    display: block;
  }
}

@media (max-width: 1250px) {
  .innerArticleForm,
  .reviewForm,
  form {
    padding: 0;
  }
  .innerArticleForm {
    width: 70%;
  }
}

@media (max-width: 557px) {
  .innerArticleForm {
    width: 100%;
  }
}
<!DOCTYPE html>

<head>

</head>

<body>
  <header id="pageHeader">
    <div class="left">
      <a href="/"><img class="logo" src="https://cruisecrunch.s3.us-east-2.amazonaws.com/logo.png"></a>
    </div>
    <div class="right">
      <p><a href="/signup">Sign up</a> | <a href="/login">Login</a></p>
    </div>
  </header>
  <article id="mainArticle">
    <div class="text">
      <h2>All Cruise Ships</h2>
      <p>Welcome to CruiseCrunch - the ultimate destination for cruise research. Enjoy free access to user reviews and ratings for cruise ships across the industry. If you have cruised before, please <a href="/addreview">leave a review</a> of your trip for
        others. </p>
    </div>
    <div class="innerArticle">
      <a href="/ship/disney-dream"><img class="mainPage" src="https://www.goport.com/blog/wp-content/uploads/2013/10/ship-photo-shopped.jpg" alt="Disney Dream"></a><br>Disney Dream<br><br><span class="stars-container stars-20">★★★★★</span><br><br><a href="/ship/disney-dream">See Reviews (1)</a>
    </div>
  </article>
  <nav id="mainNav">Navigation
    <div class="nav">
      <div class="ul">
        <div class="li"><a href="/"><i class="fa fa-ship"></i> All Ships</a></div>
        <div class="li"><a href="/addreview"><i class="fa fa-plus"></i> Add Review</a></div>
        <div class="li"><a href="/topships"><i class="fa fa-trophy"></i> Top Rated Ships</a></div>
        <div class="li"> <a href="/bestforkids"><i class="fa fa-child"></i> Best For Kids</a></div>
        <div class="li"> <a href="/bestvalue"><i class="fa fa-dollar"></i> Best Value</a></div>
        <div class="li"><a href="/cabins"><i class="fa fa-bed"></i> Best Cabins</a></div>
        <div class="li"><a href="/bestdining"><i class="fa fa-beer"></i> Best Dining</a></div>
        <div class="li"><a href="/entertainment"><i class="fa fa-film"></i> Best Entertainment</a></div>
        <div class="li"><a href="/worstships"><i class="fa fa-thumbs-down"></i> Lowest Rated Ships</a></div>
        <div class="li"><a href="/recentreviews"><i class="fa fa-rss-square"></i> Most Recent Reviews</a></div>
        <div class="li"><a href="/newships"><i class="fa fa-anchor"></i> New Ships</a></div>
        <div class="li"><br></div>
      </div>
    </div>
  </nav>
  <div id="siteAds">Over 6 cruise ship reviews!</div>
  <footer id="pageFooter">
    <p class="footer">&#169; 2020, CruiseCrunch</p>
  </footer>
</body>

https://developer.mozilla.org/en-US/docs/Web/CSS/filter

brightness()

brightness() 函数对输入图像应用线性乘法器,使其看起来或多或少亮。 0% 的值将创建一个全黑的图像。 100% 的值使输入保持不变。其他值是效果的线性乘数。允许使用 over 100% 的值,提供更明亮的结果。插值的空白值为 1。

除此之外,您还可以使用过渡并通过弹跳效果从transition-timing-functioncubic-bezier(x,x,x,x) 值增加其效果:https://www.sitepoint.com/css3-transitions-cubic-bezier-timing-function/

【讨论】:

    【解决方案2】:

    您可以将 :hover 应用在​​每个元素上,包括下面 sn-p 中的图像。然后,您可以在所需的悬停效果期间设置每个差异的样式。对于你在 css 中做不到的所有事情,你都需要 JS。

    img {
      width:50%;
      opacity: 80%;
    }
    
    img:hover {
      -webkit-box-shadow:0 0 20px blue; 
      -moz-box-shadow: 0 0 20px blue; 
      box-shadow:0 0 20px blue;
      opacity: 100%;
    }
    &lt;img src="https://www.tacoshy.de/Images/Yoshi/IMAG0735.jpg"&gt;

    【讨论】:

    • 对于box-shadow,此时几乎不需要供应商前缀。
    • opacity 有效值介于 0 和 1 之间。您可能指的是 0.8 和 1。;)
    • @G-Cyrillus 0.8 是 80%。不透明度可以写为十进制数或百分比。结果是一样的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    • 2013-11-26
    • 2011-12-02
    • 1970-01-01
    • 2020-08-27
    相关资源
    最近更新 更多