在悬停时向背景图像添加淡入和淡出过渡
我在页面的侧边栏中有一个简单的导航系统,您可以在此处查看。
正如您所看到的,当您将鼠标悬停在侧边栏中的链接之一上时,背景图像会发生变化,因为我在 CSS 中为悬停设置了不同的背景 URL。我正在寻找一种添加过渡效果的简单方法,以便悬停背景图像淡入和淡出。我尝试了几种 jQuery 方法但没有成功。我想知道是否有一种方法可以使用 jQuery 或其他方法向背景 URL 图像添加淡入淡出过渡,或者我是否必须使用不同的方法来获得过渡效果。
谢谢,
尼克
I have a simple navigation system in a sidebar on a page that you can see here.
As you can see when you hover over one of the links in the sidebar the background image changes as I have set a different background URL for hover in the CSS. I am looking for a simple way of adding a transition effect, so that the hover background image fades in and out. I have tried a couple of jQuery methods without success. I am wondering if there is a way of adding a fade transition using jQuery or some other method to the background URL image, or whether I have to use a different method to get a transition effect.
Thanks,
Nick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果没有一些说明或一些示例代码,很难说出您想要做什么,但是您是否在寻找 的东西像这样?
在此示例中,
#someElement
引用img
元素。src
属性在悬停时发生变化,并具有淡入淡出效果。编辑 - 重新阅读您的问题后,我认为您想要更改
background-image
CSS 属性,而不是src
的 <代码>img 元素。如果是这种情况,请查看此示例。真正改变的只是用css
替换 jQueryattr
函数。It's difficult to say what you're trying to do without some clarification, or some example code, but are you looking for something like this?
In this example,
#someElement
refers to animg
element. Thesrc
attribute is changed on hover, with a fade effect.Edit - having re-read your question, I think you want to change the
background-image
CSS property, rather than thesrc
of animg
element. If that's the case, have a look at this example. All that has really changed is replacing the jQueryattr
function withcss
.使用以下代码:
Use the following codes:
HTML
这是在其背景中包含图像的 HTML 元素:
CSS
我们定义了两个 CSS 类。包含
image1.jpg
的元素的默认类和包含image2.jpg
的第二个类:JavaScript
JavaScript 使用回调/承诺来确保每个转换都按正确的顺序发生:
HTML
This is the HTML element which will have the image in its background:
CSS
We define two CSS classes. The default one for the element which contains
image1.jpg
and a second class which containsimage2.jpg
:JavaScript
The JavaScript uses callbacks/promises to ensure each transition happens in the right sequence: