[英]Fading in/Fade out text in IE
I had a problem and whipped up a quick solution: fade-in and fade-out a series of quotations. 我遇到了一个问题,并提出了一个快速解决方案:淡入淡出一系列报价。 My solution works just as I want it in every browser except any of the IEs.
我的解决方案可以在除IE之外的所有浏览器中正常运行。 The only problem with the IEs is that the text does not fade in and fade out;
IE的唯一问题是文本不会淡入和淡出。 the text simply pops into existence.
文本只是突然出现。
I believe I've run into a similar problem before when trying to dynamically change the filter:alpha(opacity=xx)
of an element. 我相信在尝试动态更改元素的
filter:alpha(opacity=xx)
之前遇到了类似的问题。
Any help is greatly appreciated! 任何帮助是极大的赞赏!
<html>
<head>
<style>
.fadetext{
background:green;
border:1px solid red;
height:50px;
width:500px;
}
.fadetext div{
background:yellow;
}
</style>
<script type="text/javascript">
var CI_common = {
C:function(cls,elm){
if(!elm) elm = document;
if(document.getElementsByClassName){
return elm.getElementsByClassName(cls);
}else{
var t = [];
var o = elm.getElementsByTagName("*");
var r = new RegExp("(^|\\s)" + cls + "($|\\s)");
for(var i=0;i<o.length;i++){
if(o[i].className.match(r)) t.push(o[i]);
}
return t;
}
},
eventAdd:function(obj,evt,func){
if(obj.addEventListener){
obj.addEventListener(evt,func,false);
}else if(obj.attachEvent){
obj["x" + evt + func] = func;
obj[evt + func] = function(){
obj["x" + evt + func](window.event);
}
obj.attachEvent("on" + evt,obj[evt + func]);
}
}
}
var CI_fadetext = {
init:function(){
var c = CI_common.C("fadetext"); // Simply a getElementsByClassName function
for(var i=0;i<c.length;i++){
c[i].style.overflow = "hidden";
var kids = c[i].getElementsByTagName("div");
for(var j=0;j<kids.length;j++){
kids[j].style.display = "none";
kids[j].style.filter = "alpha(opacity=0)";
kids[j].style.opacity = "0";
(function(obj,index,len){
obj.fadetexttimeout = setTimeout(function(){
CI_fadetext.fade(obj,true);
obj.fadeininterval = setInterval(function(){
CI_fadetext.fade(obj,true);
},5000*len)
},5000*index);
setTimeout(function(){
CI_fadetext.fade(obj,false);
obj.fadeoutinterval = setInterval(function(){
CI_fadetext.fade(obj,false);
},5000*len)
},5000*index+4400);
})(kids[j],j,kids.length);
}
}
},
fade:function(elm,dir){
function fade(start){
start = (dir ? start + 10 : start - 10);
elm.style.filter = "alpha(opacity=" + start + ")";
elm.style.opacity = start/100;
document.getElementById("output").innerHTML = elm.style.filter;
if(start > 100 || start <0){
elm.style.display = (dir ? "" : "none");
elm.style.filter = "alpha(opacity=" + (dir ? 100 : 0) + ")";
elm.style.opacity = (dir ? 1 : 0);
}else elm.fadetexttimeout = setTimeout(function(){fade(start);},50);
}
if(dir){
elm.style.display = "";
fade(0);
}else fade(100);
}
}
CI_common.eventAdd(window,"load",CI_fadetext.init); // Just a window.onload level 2 event listener
</script>
</head>
<body>
<div id="output"></div>
<div class="fadetext">
<div>AAAA</div>
<div>BBBB</div>
<div>CCCC</div>
<div>DDDD</div>
<div>EEEE</div>
</div>
</body>
</html>
The code here works for me in IE 8, Firefox, and Safari: 此处的代码适用于IE 8,Firefox和Safari:
http://www.switchonthecode.com/tutorials/javascript-tutorial-simple-fade-animation http://www.switchonthecode.com/tutorials/javascript-tutorial-simple-fade-animation
It's become so cliche but yes you should also consider using a library like jQuery which can do this in 1 line. 它变得陈词滥调,但是是的,您还应该考虑使用像jQuery这样的库,它可以在1行中完成此操作。
A jQuery example: http://viralpatel.net/blogs/2009/01/fadein-fadeout-fade-out-effect-in-html-text-div-using-jquery.html 一个jQuery示例: http : //viralpatel.net/blogs/2009/01/fadein-fadeout-fade-out-effect-in-html-text-div-using-jquery.html
Set the value to the filter array rather than the style element. 将值设置为过滤器数组而不是样式元素。 Then IE be able to keep up.
然后IE就能跟上。
if (elm.filters)
elm.filters[0].opacity=value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.