简体   繁体   English

(Javascript)CSS width()属性在窗口调整大小之前不适用

[英](Javascript) CSS width() property not applied until window resize

I have a simple scrubber for playing music, using two divs, $scrubber and $progress, and increasing the width of the progress as the song progresses. 我有一个简单的洗涤器,用于播放音乐,使用两个div,$ scrubber和$ progress,并随着歌曲的进行而增加进度的宽度。

<div class="a-meter" id="scrubber" aria-label="60%" style="height:5px">
    <div class="a-meter-bar" id="progress">
    </div>
</div>

As I receive progress events, I calculate the width and then use Javascript to set $progress' width property. 当我收到进度事件时,我计算宽度,然后使用Javascript设置$ progress的width属性。

    countdownTimeout = setTimeout(updateCountdown, 100);
    var x = (interval)*(durationSeconds-secondsRemaining)+"%";
    console.log("width is "+x);
    $progress.width((interval)*(durationSeconds-secondsRemaining)+"%");

I can see these log statements as the song progresses and the % increases. 随着歌曲的前进和%的增加,我可以看到这些日志语句。 However, the progress bar width will not actually be set until the window is resized. 但是,直到调整窗口大小后,才会实际设置进度条的宽度。

It does not matter in which direction or how small of a change I make, it immediately kicks in the width and the progress bar races to catch up. 无论我朝哪个方向变化或变化有多小,它都会立即变宽,并且进度条开始追赶。

This only happens in Chrome, and only recently started without much code change to this file - Firefox works as expected and the width is set normally. 这仅在Chrome中发生,并且仅在最近才启动,并且对此文件没有太多代码更改-Firefox可以正常工作,并且宽度设置正常。

Any ideas on what I might be doing wrong or how I might fix it? 关于我可能做错了什么或如何解决的任何想法? Thank you in advance. 先感谢您。

Edit : added JSFiddle with basic idea of what the expected behavior is: https://jsfiddle.net/ympu6rt3/3/ 编辑 :添加了JSFiddle与预期行为的基本概念是: https ://jsfiddle.net/ympu6rt3/3/

I think that you problem is that you call wrong to the element from jquery $progress it should be $('#progress') , also use setInterval instead of setTimeOut due to setTimeOut is executed only one time, while setInterval run the function constantly. 我认为您的问题是您从jquery $ progress调用了错误的元素,它应该是$('#progress') ,由于setTimeOut只执行一次,所以还要使用setInterval而不是setTimeOut,而setInterval会不断运行该函数。

countdownTimeout = setInterval(updateCountdown(), 100);
function updateCountdown(){
    var x = (interval)*(durationSeconds-secondsRemaining)+"%";
    console.log("width is "+x);
    $('#progress').width((interval)*(durationSeconds-secondsRemaining)+"%");
}

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

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