我發(fā)現(xiàn)了這個問題,并試圖對其進行修復(fù),因為其背后的邏輯與我要實現(xiàn)的邏輯相似。我已經(jīng)設(shè)法以最少的編輯工作了。但它沒有按預(yù)期工作。注意:我已將點擊功能注釋掉,因為它可以正常工作。怎么了如果單擊,volumeBtn并且volumeRange在向左或向右滑動時不小心將光標(biāo)移到div的高度或?qū)挾戎?,則mouseup當(dāng)您停止單擊鼠標(biāo)時,事件監(jiān)聽器將不會執(zhí)行。像1一樣,單擊后,一旦超出“ volumeRange” div的范圍volumeBtn,就無法volumeBtn向左或向右拖動。從第零位置到所需位置存在閃爍。我想發(fā)生什么如果單擊,volumeBtn然后停止單擊鼠標(biāo),則mouseup即使光標(biāo)不再位于上,也應(yīng)執(zhí)行該事件volumeRange。如果單擊,即使光標(biāo)不再位于上,volumeBtn您也應(yīng)該能夠volumeBtn向左或向右拖動volumeRange。 const volume = document.querySelector('.volume'); const volumeRange = document.querySelector('.volume-range'); const volumeBtn = document.querySelector('.volume-button'); // volumeRange.addEventListener("click", volumeClick ); // function volumeClick(event) { // let x = event.offsetX; // volume.style.width = (Math.floor(x) + 10) + 'px'; // } let mouseIsDown = false; volumeBtn.addEventListener("mouseup", up); volumeBtn.addEventListener("mousedown", down); volumeRange.addEventListener("mousemove", volumeSlide); function down(){ mouseIsDown = true; } function up(){ mouseIsDown = false; } function volumeSlide(event) { if (mouseIsDown) { let x = event.offsetX; console.log(x); volume.style.width = Math.floor(x + 10) + 'px'; } }
構(gòu)建自定義音量滑塊
ibeautiful
2021-04-13 13:41:15