简体   繁体   English

翻转淡入会引发错误

[英]Rollover Fade In Throwing Errors

My goal is to create a rollover text display gadget. 我的目标是创建一个过渡文本显示小工具。 When the user hovers over the picture, the picture fades (opacity is turned down) then a text appears. 当用户将鼠标悬停在图片上方时,图片会褪色(不透明度降低),然后出现文本。 When the user hovers out of the picture, it toggles back to normal (pciture has opacity=1, text is gone). 当用户将鼠标悬停在图片之外时,它将切换回正常状态(照片的不透明度= 1,文本消失了)。

I tested my early version of this and when hovering the picture, I did not see any text appear. 我测试了我的早期版本,将鼠标悬停在图片上时,没有看到任何文字出现。 I ran through the debugger and got the following error: 我通过调试器,并遇到以下错误:

Uncaught TypeError: Object [object Object] has no method 'display' 

I have duplicated my small project in a JSFiddle . 我已经在JSFiddle中复制了我的小项目。 If anybody could help my text appear and remove the error, it would be appreciated. 如果有人可以帮助我显示文本并消除错误,将不胜感激。 Thanks! 谢谢!

Two things: 两件事情:

  1. jQuery doesn't have a function called display jQuery没有名为display的函数
  2. In your fiddle, the .hovertext span is not a child of .pic , so $('.hovertext', $(this)) , where $(this) is one of the images, won't select anything. 在您的小提琴中, .hovertext范围不是.hovertext的子.pic ,因此$('.hovertext', $(this)) (其中$(this)是图像之一$(this)不会选择任何内容。

I think you could use CSS3 transitions to get a similar effect: http://jsfiddle.net/derekstory/bzf6L/1/ 我认为您可以使用CSS3过渡来获得类似的效果: http : //jsfiddle.net/derekstory/bzf6L/1/

img {
    position: absolute;
    opacity:1;
    -webkit-transition: all .8s ease;
    -moz-transition: all .8s ease;
    -ms-transition: all .8s ease;
    -o-transition: all .8s ease;
    transition: all .8s ease;
}
img:hover {
    opacity : .2;
}
span {
    font-family:Arial, Verdana, sans-serif;
}
.hovertext {
    position: aboslute;
    text-align:center;
}

You should use .fadeTo() instead of .slideToggle() It will toggle itself because of the onhover state: 您应该使用.fadeTo()而不是.slideToggle() ,因为处于onhover状态,它会自动切换:

JavaScript 的JavaScript

$('.pic').hover(function() {
    $(this).fadeTo(9);
    //here is the place for the function to show the text.
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM