How to add alpha filter to any HTML element and keep the other filters in IE?

前端 未结 3 1225
误落风尘
误落风尘 2020-12-20 21:11

If I have this HTML


Then this javascript works in IE6

         


        
3条回答
  •  北荒
    北荒 (楼主)
    2020-12-20 21:33

    After some more testing, I come with this solution

    var filter = function(obj,f,params) {
      var found, nf, dx = "DXImageTransform.Microsoft.";
    
      // check if DXImageTransform.Microsoft.[Filter] or [Filter] filter is set
      try { nf = obj.filters.item(dx+f); found = true; } catch(e) {}
      if(!found) try { nf = obj.filters.item(f); found = true; } catch(e) {}
    
      // filter is set - change existing one
      if(found) {
        nf.Enabled = true; // if exists, it might be disabled
        if(params) for(var i in params) nf[i] = params[i];
      }
    
      // filter is not set - apply new one
      else {
        nf = "";
        if(params) for(var i in params) nf+= i.toLowerCase()+"="+params[i]+",";
        if(params) nf = "("+nf.substr(0,nf.length-1)+")";
        obj.style.filter+= "progid:"+dx+f+nf+" ";
      }
    
      // hasLayout property hack
      if(!obj.style.zoom) obj.style.zoom = 1;
    };
    

    Example

    var obj = document.getElementById("a");
    if(document.body.filters) filter(obj,"Alpha",{Opacity:50});
    

    I hope this works, if anybody finds a problem, please tell me.

    Sources

    obj.filters property http://msdn.microsoft.com/en-us/library/ms537452(VS.85).aspx

    filter.Alpha http://msdn.microsoft.com/en-us/library/ms532967(VS.85).aspx

提交回复
热议问题