简体   繁体   English

使用纯 Javascript 滚动到可滚动 DIV 内的元素

[英]Scrolling to a element inside a scrollable DIV with pure Javascript

I have a div that has overflow: scroll and I have some elements inside the DIV that are hidden.我有一个overflow: scroll的 div overflow: scroll并且我在 DIV 中有一些隐藏的元素。 On click of a button on the page, I want to make the DIV scroll to a particular element inside the DIV.单击页面上的按钮时,我想让 DIV 滚动到 DIV 内的特定元素。

How do I achieve this?我如何实现这一目标?

You need to read the offsetTop property of the div you need to scroll to and then set that offset to the scrollTop property of the container div .您需要读取需要滚动到的 div 的offsetTop属性,然后将该偏移设置为容器divscrollTop属性。 Bind this function the event you want to :将此函数绑定到您想要的事件:

 function scrollToElementD(){ var topPos = document.getElementById('inner-element').offsetTop; document.getElementById('container').scrollTop = topPos-10; }
 div { height: 200px; width: 100px; border: 1px solid black; overflow: auto; } p { height: 80px; background: blue; } #inner-element { background: red; }
 <div id="container"><p>A</p><p>B</p><p>C</p><p id="inner-element">D</p><p>E</p><p>F</p></div> <button onclick="scrollToElementD()">SCROLL TO D</button>

function scrollToElementD(){
  var topPos = document.getElementById('inner-element').offsetTop;
  document.getElementById('container').scrollTop = topPos-10;
}

Fiddle : http://jsfiddle.net/p3kar5bb/322/ (courtesy @rofrischmann)小提琴: http : //jsfiddle.net/p3kar5bb/322/ (礼貌@rofrischmann)

Just improved it by setting a smooth auto scrolling inside a list contained in a div只是通过在 div 中包含的列表中设置平滑自动滚动来改进它

https://codepen.io/rebosante/pen/eENYBv https://codepen.io/rebosante/pen/eENYBv

var topPos = elem.offsetTop

document.getElementById('mybutton').onclick = function () {
   console.log('click')
  scrollTo(document.getElementById('container'), topPos-10, 600);   
}

function scrollTo(element, to, duration) {
    var start = element.scrollTop,
        change = to - start,
        currentTime = 0,
        increment = 20;

    var animateScroll = function(){        
        currentTime += increment;
        var val = Math.easeInOutQuad(currentTime, start, change, duration);
        element.scrollTop = val;
        if(currentTime < duration) {
            setTimeout(animateScroll, increment);
        }
    };
    animateScroll();
}

//t = current time
//b = start value
//c = change in value
//d = duration
Math.easeInOutQuad = function (t, b, c, d) {
    t /= d/2;
    if (t < 1) return c/2*t*t + b;
    t--;
    return -c/2 * (t*(t-2) - 1) + b;
};

I guess it may help someone :)我想它可能会帮助某人:)

Here's a simple pure JavaScript solution that works for a target Number (value for scrollTop ), target DOM element, or some special String cases:这是一个简单的纯 JavaScript 解决方案,适用于目标 Number( scrollTop值)、目标 DOM 元素或一些特殊的 String 情况:

/**
 * target - target to scroll to (DOM element, scrollTop Number, 'top', or 'bottom'
 * containerEl - DOM element for the container with scrollbars
 */
var scrollToTarget = function(target, containerEl) {
    // Moved up here for readability:
    var isElement = target && target.nodeType === 1,
        isNumber = Object.prototype.toString.call(target) === '[object Number]';

    if (isElement) {
        containerEl.scrollTop = target.offsetTop;
    } else if (isNumber) {
        containerEl.scrollTop = target;
    } else if (target === 'bottom') {
        containerEl.scrollTop = containerEl.scrollHeight - containerEl.offsetHeight;
    } else if (target === 'top') {
        containerEl.scrollTop = 0;
    }
};

And here are some examples of usage:以下是一些用法示例:

// Scroll to the top
var scrollableDiv = document.getElementById('scrollable_div');
scrollToTarget('top', scrollableDiv);

or或者

// Scroll to 200px from the top
var scrollableDiv = document.getElementById('scrollable_div');
scrollToTarget(200, scrollableDiv);

or或者

// Scroll to targetElement
var scrollableDiv = document.getElementById('scrollable_div');
var targetElement= document.getElementById('target_element');
scrollToTarget(targetElement, scrollableDiv);

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

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