简体   繁体   English

可滚动 DIV 可以更新范围 Slider 吗?

[英]Can a scrollable DIV update a Range Slider?

I have a range slider that is scrolling the content in a DIV.我有一个范围 slider 正在滚动 DIV 中的内容。

If you click on the scrollable area you will see that you can drag the the scrollable DIV to the left and right.如果单击可滚动区域,您将看到可以左右拖动可滚动 DIV。 However, when you do this the range is not updated.但是,当您这样做时,范围不会更新。 Is there a way to update the range when the DIV is manually scrolled?手动滚动 DIV 时有没有办法更新范围? I have tried to get the onscroll() position to update the range with no luck.我试图让 onscroll() position 更新范围但没有运气。

https://jsfiddle.net/esoteric/fxtonew7/5/ https://jsfiddle.net/esoteric/fxtonew7/5/

//Range Slider
    var scroll = document.getElementById("scroll-range");
    var panel = document.getElementById("scrolling-container");

    var total = panel.scrollWidth - panel.offsetWidth;
    var percentage = total * (this.value / 100);
    var percentageWidth = total / 10; //10% for each +/- click

    scroll.oninput = function() {

        panel = document.getElementById("scrolling-container");
        total = panel.scrollWidth - panel.offsetWidth;
        percentage = total * (this.value / 100);
        panel.scrollLeft = percentage;
    }

    //Foward Arrow
    document.getElementById("increase").addEventListener("click",
        function() {
            scroll.stepUp(10);
            panel.scrollBy(percentageWidth, 0)
        });

    //Back Arrow
    document.getElementById("decrease").addEventListener("click",
        function() {
            scroll.stepUp(-10);
            panel.scrollBy(-percentageWidth, 0)
        });

        
    //Desktop grab and scroll
    const slider = document.getElementById("scrolling-container");
    let isDown = false;
    let startX;
    let scrollLeft;

    slider.addEventListener('mousedown', (e) => {
        isDown = true;
        slider.classList.add('active');
        startX = e.pageX - slider.offsetLeft;
        scrollLeft = slider.scrollLeft;
    });

    slider.addEventListener('mouseleave', () => {
        isDown = false;
        slider.classList.remove('active');
    });

    slider.addEventListener('mouseup', () => {
        isDown = false;
        slider.classList.remove('active');
    });

    slider.addEventListener('mousemove', (e) => {
        if (!isDown) return;
        e.preventDefault();
        const x = e.pageX - slider.offsetLeft;
        const walk = (x - startX) * 3;
        slider.scrollLeft = scrollLeft - walk;
    });

You need to calculate the opposite of what you do for when your drag the slider您需要计算与拖动 slider 时所做的相反的事情

panel.addEventListener('scroll', (e)=>{
    total = panel.scrollWidth - panel.offsetWidth;
    percentage = panel.scrollLeft / total
    scroll.value = percentage * 100
})

updated fiddle: https://jsfiddle.net/gaby/dwvtLn4g/8/更新的小提琴: https://jsfiddle.net/gaby/dwvtLn4g/8/

What you would have to do is test wherein the "panel" where you scrolling to and update the scrollbar position您需要做的是测试您滚动到的“面板”并更新滚动条 position

you can do that by adding another panel event listener您可以通过添加另一个面板事件侦听器来做到这一点

    panel.addEventListener('mousemove', (e) => {
    // get scroll position in px
    console.log(panel.scrollLeft, panel.scrollTop);
    });

This will print the left position in the console这将在控制台中打印左侧的 position

Listen for scroll events on your div, then find out how much percent of the scroll was done by dividing the scroll position with the possible max scroll and then set the value (as your range goes from 0 to 100) to that percentage监听 div 上的滚动事件,然后通过将滚动 position 除以可能的最大滚动,然后将值(范围从 0 到 100)设置为该百分比,找出滚动的百分比

    slider.addEventListener('scroll', e => {
       const maxScrollLeft = slider.scrollWidth - scroll.clientWidth
       const percentage =  slider.scrollLeft / maxScrollLeft
       scroll.value = percentage * 100 // as percentage is normalized from 0 to 1
    })

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

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