Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
Asp.net Core JQuery scroll to div on page load not working

I have run up against a little problem. I am trying to get the page to scroll to a specific point on page load but I'm not getting anywhere. The code I have used is below. For some reason it doesn't seem to be running at all.

<script type="text/javascript">
        // scroll past header on page load
        function scrollToDiv() {
            $(document).ready(function () {
                var ele = $('html, body').animate({ scrollTop: $('#tree').position().top }, 500, 'linear');
            });
        };


        scrollToDiv();

    </script>

If you have any suggestions as to why it is not working I would certainly appreciate it.

I am building using ASP.NET Core. The above jQuery code worked on an old webforms project so I have no idea why it isn't working now.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

To do this with jQuery:

<script>
$(function(){
    const top = $('#tree').position().top;
    $('html, body').animate({ scrollTop: top }, 500, 'linear');
});
</script>

However, you don't need jQuery for this:

<script>
window.addEventListener('DOMContentLoaded', () => {
    const target = document.getElementById('tree');
    target.scrollIntoView({ behavior: 'smooth', block: 'start' });
});
</script>

Window: DOMContentLoaded event - Web APIs | MDN
Element.scrollIntoView() - Web APIs | MDN

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!