Setting opacity of html element using Javascript

文章源自:http://viralpatel.net/blogs/setting-opacity-of-html-element-using-javascript/

Setting opacity of html element using Javascript

You must have seen the blur effect of html elements like table, div where the opacity of these elements are slowly made to transparent or opaque. This can be acheived through javascript.

Internet Exlporer supports a CSS attribute called filter, wherein you can apply lot of different filter options available in browser. There are currently three categories of filters – Visual filters and Reveal/Blend Transition filters. Multiple filters can be applied to a selector to produce interesting results, with the order of application often playing an important role in the final visual result. Current filters only apply in a visual context, but the extensibility of the property could allow for other capabilities.

Hence following code can be used to set opacity of an html element in IE.

var opacityValue = 0.5; //make it 50% opaque
myElement.style.filter = "alpha(opacity=" + opacityValue*100 + ")"; // IE

 But the above code will not work with other browsers like Firefox, Opera etc. Firefox supports another CSS property called opacity which controls the opacity of an element.
Following javascript function will set the opacity of any html element irrespective of the browser.

function setOpacity (myElement, opacityValue) {
    if (window.ActiveXObject) {
        myElement.style.filter = "alpha(opacity="
             + opacityValue*100 + ")"; // IE
    } else {
        myElement.style.opacity = opacityValue; // Gecko/Opera
    }
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值