[ tag使用时如何制作幻灯片效果

问题描述 投票:-1回答:1

我想在我的网站上点击一个按钮时制作幻灯片效果,它会直接转到我选择的div。我不知道该怎么做,也找不到合适的视频或文章。

代码(我只显示了重要部分):

<!DOCTYPE html>
<html>
  <head>
    <title>Internet - Part 3<title>
  </head>
  <body>
    <div id='header'>
      <a href='#section'>Section</a>
    </div>
    <div id='section'>

      Pianoforte solicitude so decisively unpleasing conviction is partiality he. Or particular so 
      diminution entreaties oh do. Real he me fond show gave shot plan. Mirth blush linen small hoped way 
      its along. Resolution frequently apartments off all discretion devonshire. Saw sir fat spirit 
      seeing valley. He looked or valley lively. If learn woody spoil of taken he cause. 

    </div>
  </body>
</head>
html animation slide
1个回答
1
投票

没有jQuery:

document.querySelectorAll('a[href^="#"]').forEach(anchor => {
    anchor.addEventListener('click', function (e) {
        e.preventDefault();

        document.querySelector(this.getAttribute('href')).scrollIntoView({
            behavior: 'smooth'
        });
    });
});

更多信息here


0
投票

尝试此:

 $('#header > a').click(function(event){
    event.preventDefault();
    var id = $(this).attr('href');
    $('html, body').animate({
      scrollTop: $(id).offset().top;
    }, 600);
 })
© www.soinside.com 2019 - 2024. All rights reserved.