V
V
Vladimir Golub2019-06-27 14:07:18
React
Vladimir Golub, 2019-06-27 14:07:18

Why can't I get value from styles?

In componentDidMount() I run a function to get the styles of the active slide.

handlerCalculateWidthSlides = () => {
        let slideActive = this._slider.innerSlider.list.querySelector('.slick-current');

        let stylesActiveSlide = getComputedStyle(slideActive);

        console.log(stylesActiveSlide); 
        console.log(stylesActiveSlide.width);
    };

I look at the first console log, height 900, in the second it displays the entire width of the screen.
I do it like this:
console.log(slideActive);

        // <div data-index="0" className="slick-slide slick-active slick-center slick-current" tabIndex="-1" aria-hidden="false" style="outline: none; width: 900px;">

        console.log(slideActive.style.width)
        
        // 20%

Where does 20% come from, I don’t understand?
I am using react-slick.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-06-27
@RazerVG

slideActive.style.width is the value given in the style attribute, and getComputedStyle returns the actual values ​​computed by the browser.
Most likely, the library changes the value of width at runtime. Try to open the developer panel and see the values ​​​​at a particular point in time there. There shouldn't be any magic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question