Цитата:
наверно надо сначала показать блок two потом этот блок проскролить к нужному li |
Цитата:
p.s Из за этого Вы посчитали код бредом? :blink: |
Lemme,
я непонимаю как вы его скролите и что такое getBoundingClientRect().y |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> body{ height: 1500px; } #two { margin-top: 445px; width: 100%; height: 150px; overflow: auto; } li { display: block; height: 150px; color: #fff; text-align: center; } </style> </head> <body> <div id="two"> <ul> <li style="background-color: tomato">1</li> <li style="background-color: purple">2</li> <li style="background-color: blue">3</li> </ul> </div> <script> function showItem(x) { var two = document.getElementById('two'), item = two.querySelectorAll('li')[x-1]; item && item.scrollIntoView() } showItem(3); </script> </body> </html> |
рони, позиция элемента - аля offsetTop
Цитата:
|
или так
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> body{ height: 1500px; } #two { margin-top: 445px; width: 100%; height: 150px; overflow: auto; } li { display: block; height: 150px; color: #fff; text-align: center; } </style> </head> <body> <div id="two"> <ul> <li style="background-color: tomato">1</li> <li style="background-color: purple">2</li> <li style="background-color: blue">3</li> </ul> </div> <script> function showItem(x) { var two = document.getElementById('two'), item = two.querySelectorAll('li')[x-1]; window.scrollTo(0,two.getBoundingClientRect().top) two.scrollTop = item.getBoundingClientRect().top } window.onload = function() { showItem(3); } </script> </body> </html> |
рони, ну, ТС не уточнил, на счет видимости блока two, поэтому я не скроллил window.
|
Lemme,
вы так и не ответили что такое что такое getBoundingClientRect().y , на всякий случай: оба ваших варианта у меня не работают. |
Цитата:
Цитата:
Цитата:
DOMRect { x: 48, y: 174, width: 608, height: 500, top: 174, right: 656, bottom: 674, left: 48 } |
Lemme,
:) ок |
Часовой пояс GMT +3, время: 23:08. |