search

Home  >  Q&A  >  body text

javascript - js div is set to overflow: scroll; the li tag specifies scrolling to a certain position

window.scrollTo(0, 300); This is for scrolling the window, but scrolling the li label below,
For example, scrolling li[9] to the top, what should be done?

        <p id="example-2"> 
            <p>这是标题</p>
            <p id="p1" style="height: 5rem;border: 1px solid #007AFF;overflow: scroll;">
                <ul>
                    <li>1</li>
                    <li>2</li>
                    <li>3</li>
                    <li>4</li>
                    <li>5</li>
                    <li>6</li>
                    <li>7</li>
                    <li>8</li>
                    <li>9</li>
                    <li>10</li>
                    <li>11</li>
                    <li>12</li>
                </ul>
            </p>
        </p>
伊谢尔伦伊谢尔伦2755 days ago813

reply all(1)I'll reply

  • 仅有的幸福

    仅有的幸福2017-05-19 10:36:26

    p.addEventListener('scroll', function (){
        //...
    })

    Set the scrollTop of p to the offsetTop of li


    Code address

    reply
    0
  • Cancelreply