精通JavaScript源码3

本文介绍了通过JavaScript来实现网页元素的隐藏与显示,以及如何调整元素的透明度。提供了两个实用函数,分别是用于隐藏元素的`hide`函数和显示元素的`show`函数,并且给出了一种设置元素透明度的方法。

设置元素的隐藏或者可见:

// A function for hiding (using display) an element
function hide( elem ) {
    // Find out what it’s current display state is
    var curDisplay = getStyle( elem, ‘display’ );
    //  Remember its display state for later
    if ( curDisplay != ‘none’ )
        elem.$oldDisplay = curDisplay;
    // Set the display to none (hiding the element)
    elem.style.display = ‘none’;
}
// A function for showing (using display) an element
function show( elem ) {
    // Set the display property back to what it use to be, or use
    // ‘block’, if no previous display had been saved
    elem.style.display = elem.$oldDisplay || ‘block’;
}

   设置元素的透明度:

// Set an opacity level for an element
// (where level is a number 0-100)
function setOpacity( elem, level ) {
    // If filters exist, then this is IE, so set the Alpha filter
    if ( elem.filters )
        elem.filters.alpha.opacity = level;

    // Otherwise use the W3C opacity property
    else
        elem.style.opacity = level / 100;
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值