最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - .NET MVC application scrolls back to the caller button. How to prevent it - Stack Overflow

programmeradmin3浏览0评论

I have a section in my .cshtml and when a button is clicked on that page it scrolls to the 'contact-form' section of the page. And this is exactly I want that.

But the problem is in my MVC application it's going back to the button section where the button was clicked. Initially I should not see any movement when the button is clicked but I check by putting the breakpoint.

function scrollToContactForm(event) {
    event.preventDefault();

    document.getElementById('contact-form').scrollIntoView({
        behavior: 'smooth'
    });
}

There I could see that it's actually going to that section contact-form. But after coming out of this method it goes to the button section again. Why?

<section class="section-contact-form" id="contact-form">
  <div class="contact-form"> 
  </div>        
</section>

Button html

<button onclick="scrollToContactForm(event)" class="banner-btn">Click Me</button>

And one thing I can say is that in my script nothing was written for scrolling back to caller position. And it's not scrolling back to the top, it's scrolling to the exact same position where the button was clicked.

I tried the same code in a plain .html file it worked but when I copied and pasted the same in my .cshtml page it behaves like this.

发布评论

评论列表(0)

  1. 暂无评论