Итак, у меня есть код для события onclick, который вызовет плавную прокрутку в обычном (без jquery) Javascript к элементу с заданным идентификатором, теперь я хочу реализовать его в модальном режиме.
function scrollTo(element, to, duration) {
if (duration <= 0) return;
var difference = to - element.scrollTop;
var perTick = difference / duration * 10;
setTimeout(function() {
element.scrollTop = element.scrollTop + perTick;
if (element.scrollTop === to) return;
scrollTo(element, to, duration - 10);
}, 10);
}
elmnt = document.getElementById("example");
scrollTo(document.body, elmnt.offsetTop, 600);
Для элемента с id = «пример». У меня есть модальное окно с id = "myModal" со свойством CSS overflow-y : scroll, и когда срабатывает событие клика, я бы хотел, чтобы модальное окно плавно прокручивалось до элемента id = "poleTimeDiv".
Любые идеи?



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Использование библиотеки плавной прокрутки без Jquery
body {
height: 700px;
}<script src = "https://cdn.jsdelivr.net/gh/cferdinandi/[email protected]/dist/smooth-scroll.polyfills.min.js"></script>
<a data-scroll href = "#bazinga">Click here</a>
<div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
<div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
<div id = "bazinga">Bazinga!</div>
<script>
var scroll = new SmoothScroll('a[href* = "#"]', {
speed: 500,
speedAsDuration: true
});
</script>Взгляните, для этого есть собственная функция CSS: scroll-behavior.
CSS
html {
scroll-behavior: smooth;
}
ЯВАСКРИПТ
window.scroll({
top: 2500,
left: 0,
behavior: 'smooth' <pre rel = "HTML"><code markup = "tt" class = "language-markup">
});
// Scroll certain amounts from current position
window.scrollBy({
top: 100, // could be negative value
left: 0,
behavior: 'smooth'
});
// Scroll to a certain element
document.querySelector('.hello').scrollIntoView({
behavior: 'smooth'
});
ссылка: https://css-tricks.com/snippets/jquery/smooth-scrolling/
просто чтобы мы все знали, это делает свое дело.
CSS
.scrollable-content {
scroll-behavior:smooth;
}
Javascript
document.querySelector('poleTimeDiv').scrollIntoView({ behavior: 'smooth' });
поведение прокрутки: Smooth не поддерживается в Safari
Я нашел решение по этой ссылке, так что спасибо. Я не мог заставить вышеперечисленное работать, но в конце концов добрался туда.