简体   繁体   English

在JS中获取用户定义的CSS值

[英]Get user-defined CSS values in JS

I have two functions called slideDown, slideUp. 我有两个函数,称为slideDown,slideUp。

When I want to use the slideUp function, it's very convenient because I set the height, margin, padding values to 0. 当我想使用slideUp函数时,这非常方便,因为我将高度,边距,填充值设置为0。

But it's hard to use slideDown! 但是,很难使用slideDown! Take a look at the following: (Unfortunately I can not preview the code) 看一下以下内容:(很遗憾,我无法预览代码)

Element.prototype.addAnimate = function(animateObj,timer=500,callback,hidden=false) {
    let oldStyle = {};
    let newStyle = {};
    let thisElement = this;
    this.style.display = 'block';
    if (typeof arguments[1] == 'function') {
        timer = 500;
        callback = arguments[1];
    } else if (typeof arguments[1] == 'number') {
        timer = timer;
        callback = callback;
    }
    for (styleName in animateObj) {
        let oldStyleValue = window.getComputedStyle(this,null).getPropertyValue(styleName.replace(/[A-Z]/g,'-$&').toLowerCase());
        oldStyle[styleName] = oldStyleValue;
        newStyle[styleName] = animateObj[styleName];
    }
    let animation = this.animate([oldStyle,newStyle],{
        duration:timer,
        fill:'forwards'
    });
    animation.onfinish = () => {
        if (callback && typeof callback == 'function') {
            callback();
        }
        if (hidden == true) {
            thisElement.style.display = 'none';
        }
    }
}

(The addAnimate function is used to set animate on elements that uses api animation ) addAnimate函数用于在使用api动画的元素上设置动画

Element.prototype.slideUp = function() {
    let timer = (typeof arguments[0] == 'number') ? arguments[0] : 250;
    let callback = (typeof arguments[1] == 'function') ? arguments[1] : arguments[0];
    this.addAnimate({
        overflow:'hidden',
        height:'10px',
        marginTop:0,
        marginRight:0,
        marginBottom:0,
        marginLeft:0,
        paddingTop:0,
        paddingRight:0,
        paddingBottom:0,
        paddingLeft:0,
    },timer,callback,false);
}

(As I said, using the slideUp function is not a problem for me) (正如我所说,使用slideUp功能对我来说不是问题)

Element.prototype.slideDown = function() {
    let timer = (typeof arguments[0] == 'number') ? arguments[0] : 250;
    let callback = (typeof arguments[1] == 'function') ? arguments[1] : arguments[0];
    this.addAnimate({
        height:'auto',
        marginTop:'auto',
        marginRight:'auto',
        marginBottom:'auto',
        marginLeft:'auto',
        paddingTop:'auto',
        paddingRight:'auto',
        paddingBottom:'auto',
        paddingLeft:'auto',
    },timer,callback);
}

The main problem with this function is how to get values that are already set (such as the slideDown function in Jquery). 此函数的主要问题是如何获取已设置的值(例如Jquery中的slideDown函数)。 When I set the values to the auto , the styles apply without animation. 当我将值设置为auto ,样式将应用而不会产生动画。 (I do not want to use Jquery)

The trick here is to keep the default styles value of your element somewhere before the first update. 这里的技巧是第一次更新之前将元素的默认样式值保留在某个位置。 You could also use element.removeAttribute('style') ( doc here ) 您也可以使用element.removeAttribute('style')doc在这里

Here's a basic example: 这是一个基本示例:

 let toggle = false; const elem = document.querySelector('.no-auto'); const defaultStyles = { // Get default styles width: elem.style.width, height: elem.style.height, backgroundColor: elem.style.backgroundColor, }; function toggleAnimate() { toggle = !toggle; const styles = elem.style; if (toggle) { styles.width = '60px'; styles.height = '50px'; styles.backgroundColor = 'blue'; } else { styles.width = defaultStyles.width; styles.height = defaultStyles.height; styles.backgroundColor = defaultStyles.backgroundColor; } } elem.addEventListener('click', toggleAnimate); 
 .no-auto { width: 120px; height: 100px; background-color: red; transition: all 0.3s ease; } 
 <div class="no-auto"> </div> 

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

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