使用 Jquery 使图像出现在悬停时
我在网上环顾四周,找不到关于我想用悬停导航做什么的具体答案或教程;我发现最多的是用于图像动画或背景图像动画的 jquery 编码。
我目前的导航设置为悬停时淡入不同的颜色。我还想做的是,当有人将鼠标悬停在导航上时,链接不仅会淡出为不同的颜色(我正在工作),而且我还想要一个箭头图像,我必须出现在文本的左侧。我希望它也能随着文字一起消失,但如果这是不可能的,我也不挑剔。澄清一下,我不希望当链接未悬停时显示箭头。
如果它有帮助的话,这是我当前的褪色jquery:
this.fadeLinks = function() {
var selector = "#nav1 a";
var speed = "slow"
var bgcolor = "#6c919a";
$(selector).each(function(){
$(this).css("position","relative");
var html = $(this).html();
$(this).html("<span class=\"one\">"+ html +"</span>");
$(this).append("<span class=\"two\">"+ html +"</span>");
if($.browser.msie){
$("span.one",$(this)).css("background",bgcolor);
$("span.two",$(this)).css("background",bgcolor);
$("span.one",$(this)).css("opacity",1);
};
$("span.two",$(this)).css("opacity",0);
$("span.two",$(this)).css("position","absolute");
$("span.two",$(this)).css("top","0");
$("span.two",$(this)).css("left","0");
$(this).hover(
function(){
$("span.one",this).fadeTo(speed, 0);
$("span.two",this).fadeTo(speed, 1);
},
function(){
$("span.one",this).fadeTo(speed, 1);
$("span.two",this).fadeTo(speed, 0);
}
)
});
};
$(document).ready(function(){
fadeLinks();
});
这是导航的css:
#nav1 {
width:730px;
top: -380px;
left: -2px;
font-size:20px;
font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
color:#ffffff;
padding-top:6px;
padding-bottom:10px;
position:relative;
text-align:center;
line-height:30px;
}
#nav1 a{
font-size:30px;
font-family: Tahoma, Geneva, sans-serif;
color:#fff;
text-decoration:none;
}
#nav1 a span.two{
color:#069;
cursor:pointer;
}
我想要显示的箭头图像是“images/arrow.png”。我相信对于这种类型的事情我需要将 png bug 放入我的 IE 编码中,是吗?另外,如果我需要为此在实际的 html 中添加一些内容,请告诉我。提前致谢!
I looked around online and could not find a specific answer or tutorial on what I would like to do with my hover navigation; The most I found was jquery coding for an image animation or background image animation.
My navigation at the moment is set up with a fade to a different color over hover at the moment. What I would like to do also is that when someone hovers over the navigation, not only does the link fade to a different color (Which I have working), but I also want a arrow image I have to appear to the left of the text. I'd love for it to fade with the text too, but if thats impossible, I'm not picky. To clarify, I do not want the arrow to be shown when the link is not being hovered over.
If it helps at all, here is the current fading jquery I have:
this.fadeLinks = function() {
var selector = "#nav1 a";
var speed = "slow"
var bgcolor = "#6c919a";
$(selector).each(function(){
$(this).css("position","relative");
var html = $(this).html();
$(this).html("<span class=\"one\">"+ html +"</span>");
$(this).append("<span class=\"two\">"+ html +"</span>");
if($.browser.msie){
$("span.one",$(this)).css("background",bgcolor);
$("span.two",$(this)).css("background",bgcolor);
$("span.one",$(this)).css("opacity",1);
};
$("span.two",$(this)).css("opacity",0);
$("span.two",$(this)).css("position","absolute");
$("span.two",$(this)).css("top","0");
$("span.two",$(this)).css("left","0");
$(this).hover(
function(){
$("span.one",this).fadeTo(speed, 0);
$("span.two",this).fadeTo(speed, 1);
},
function(){
$("span.one",this).fadeTo(speed, 1);
$("span.two",this).fadeTo(speed, 0);
}
)
});
};
$(document).ready(function(){
fadeLinks();
});
Here is the css for the navigation:
#nav1 {
width:730px;
top: -380px;
left: -2px;
font-size:20px;
font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
color:#ffffff;
padding-top:6px;
padding-bottom:10px;
position:relative;
text-align:center;
line-height:30px;
}
#nav1 a{
font-size:30px;
font-family: Tahoma, Geneva, sans-serif;
color:#fff;
text-decoration:none;
}
#nav1 a span.two{
color:#069;
cursor:pointer;
}
The arrow image I want to appear is "images/arrow.png". I believe for this type of thing I'd need to put the png bug in my coding for IE, yes? Also, if I'd need to put something in my actual html for this, let me know. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
“阴天”,
下面应该可以满足您的要求。它比我之前的代码稍微复杂一些,因为它试图使文本垂直和水平居中。让我知道你的进展如何。
CSS、jQuery Javascript 和 HTML 是:
'Cloudy',
Below should give you what you are asking for. It is a bit more complicated than my earlier code as it tries to centre the text, both vertically and horizontally. Let me know how you get on with it.
The CSS, jQuery Javascript and HTML are:
我建议在代码中放置一个空的 span 或 div 容器来保存箭头图像。然后,您可以使用 JQuery 选择器在链接上调用悬停事件时淡入这些选择器。
您的代码中已经有 fadeTo ,我猜测是链接,只需在其中添加代码即可淡入箭头,并使用 CSS 选择器来表示箭头的 span 或 div。
I would suggest putting an empty span or a div container in your code to hold the arrow image. You can then use a JQuery selector to fade those in when the hover event is called on the links.
You already have fadeTo in your code for what I'm guessing are the links, just add the code there to fade in the arrows as well using the CSS selector for the spans or divs that will represent the arrows.
“多云”,
下面是一些与我使用的类似的简单代码。如果箭头图像的高度小于或等于链接中文本的高度,则效果很好。
也许您可以修改它以适合您的目的。顺便说一句,请注意单引号 (') 和双引号 (") 的使用,这消除了必须“转义”双引号的情况。
这消除了“跨度”、绝对定位和复制HTML 等。
问候 Neil
'Cloudy',
Below is some simple code similar to what I use. It works well if the height of your arrow image is less than or equal to the height of the text in the link.
Perhaps you could modify it to suit your purposes. As an aside, note the use of single quotes (') and double-quotes (") which does away with having to "escape" your double-quotes.
This does away with the "span"s, absolute positioning and having to copy the HTML etc.
Regards Neil
这将是我关于这个主题的最后一句话。如果您不反对使用表格代替 div,那么有一个更简单的解决方案。 CSS 假定图像高 32 像素。如果您的箭头图像的高度不同,您将必须更改它。
问候
尼尔
This will be my last word on this subject. If you are not averse to using a table instead of divs then there is a much simpler solution. The CSS assumes an image 32px high. You will have to alter it if your arrow image is a different height.
Regards
Neil