网站.am-secta元素悬停渐变(Hover gradient)与背景图平滑进出过渡(smooth in and out transition)效果优化求助
Fixing Smooth Hover Transition for .am-secta Element
Let's resolve that flickering transition issue you're facing. The root causes here are:
- The background image is only loaded when the hover triggers, leading to a delay/flicker as the browser fetches it.
- Your current
transitionproperty doesn't target the background attributes that change on hover, so there's no smooth animation between states.
Here's the revised CSS that maintains your element's position/size while adding a seamless hover transition:
body{ background-color: #212121; } .iframe-container { padding-top: 29.25%; position: relative; } .iframe-container iframe { border: 0; height: 360px; left: 15%; position: absolute; top: 0px; width: 70%; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15); transition: box-shadow 0.3s ease-in-out; } .iframe-container iframe:hover { box-shadow: 0 5px 20px rgb(0 0 0 / 160%); } .m-containera { width: 100%; max-width: 1517px; margin: 0 auto; align-content: center; text-align: center; } /* Modified .am-secta base styles */ .am-secta { padding-top: 40px; padding-bottom: 40px; background-color: #212121; /* Preload the background image and set initial transparent gradient */ background-image: linear-gradient(180deg, rgba(33,33,33,1) 0%, rgba(29,185,84,0) 100%), url("http://www.piyushcares.com/wp-content/uploads/2018/11/homepagevid.png"); background-repeat: no-repeat; background-position: center center top; background-size: cover; z-index: 1; /* Transition all relevant background properties + color */ transition: background-image 0.3s ease-in-out, color 0.3s ease-in-out; } /* Modified hover state - only adjust the gradient opacity */ .am-secta:hover{ background-image: linear-gradient(180deg, rgba(33,33,33,1) 0%, rgba(29,185,84,0.5) 100%), url("http://www.piyushcares.com/wp-content/uploads/2018/11/homepagevid.png"); } .contenta { padding: 0 10px; } .titls { font-size: 34px; font-weight: 700; text-align: center; color: white; font-family: 'Raleway'; } .par { color: white; text-align: center; font-weight: normal; font-size: 20px; line-height: 1.5; padding-top: 10px; padding-bottom: 45px; font-family: 'Raleway'; } .inlin { display: none; } .bold { font-weight: 700; }
Key Changes Explained:
- Preload the Background: We added the full background image + gradient to the base
.am-sectastyle, but set the green gradient's opacity to0. This ensures the image loads immediately with the page, not on hover, eliminating flicker. - Targeted Transition: Updated the
transitionproperty to includebackground-image(since that's what changes when we adjust the gradient opacity), along with your existingcolortransition. - Simplified Hover State: The hover state only modifies the gradient's opacity from
0to0.5—all other background properties stay identical, so the browser can animate the change smoothly without layout shifts.
This approach keeps your element's position and dimensions intact while delivering a seamless hover transition with no flickering.
内容的提问来源于stack exchange,提问作者Arun Prakash
相关产品推荐
相关产品推荐

