平滑滚动根本不平滑;平滑滚动仅滚动多个像素,而不滚动到指定的div

时间:2019-12-18 15:33:29

标签: javascript jquery css

我正在尝试添加一种像这样的单击平滑滚动效果:https://michalsnik.github.io/aos/

我已阅读以下内容:Smooth scroll to specific div on click,但无法适应它。我不明白scrollTop: $("#page2").offset().top的作用。

我的问题是滚动条“捕捉” 。那可能是因为我在容器上应用了scroll-snap

此外,当您位于页面之间并点击向下滚动箭头时,它会向上或向下

每当我按下该箭头时,我都希望以全屏显示第二页。应该向下滚动,直到#page2拥有height: 100vh或它占据了整个视口。

// eliminate scroll-bar
var child = document.getElementById('child-container');
child.style.right = child.clientWidth - child.offsetWidth + "px";

//scroll down effect on scroll-down-arrow
$(".scroll-down-arrow").click(function() {
    $('html,body,#child-container').animate({scrollTop: $("#page2").offset().top}, 'slow', 'linear');
});
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
  margin: 0;
	padding: 0;
	border: 0;
	font-size: 100%;
	font: inherit;
	vertical-align: baseline;
}

/* *** index.html - START *** */

body, html {
    height: 100%;
    width: 100%;
    overflow: hidden;
}

#parent-container {
    height: 100%;
    width: 100%;
    overflow: hidden;
    position: relative;
}
#child-container {
    position: absolute;
    top: 0px;
    bottom: 0px;
    left: 0px;
    right: 0px; /* exact value is given through JavaScript */
    overflow: auto;
    scroll-snap-type: both proximity;
}

header {    
    height: 100%;
    background-color: grey;
    background-attachment: fixed;
    background-position: bottom center;
    background-repeat: no-repeat;
    background-size: cover;
    text-align: center;
    scroll-snap-align: center;
}

header h1 {
    font-size: 32px;
    font-weight: bold;
    position: sticky;
    top: 5%;
    margin-bottom:10px;
}

header p {
    position: sticky;
    width: 450px;
    text-align: center;
    margin: auto;
    margin-top: 100px;
    font-size: 1.5em;
}

header .scroll-down-arrow {
	position: absolute;
	left: 50%;
	bottom: 20px;
	display: block;
	text-align: center;
	font-size: 20px;
	z-index: 100;
	text-decoration: none;
	text-shadow: 0;
    width: 30px;
    height: 30px;
    border-bottom: 2px solid #fff;
    border-right: 2px solid #fff;
    left: 50%;
    transform: translate(-50%, 0%) rotate(45deg);
    animation: fade_move_down 3s ease-in-out infinite;
    cursor: pointer;
}

/*animated scroll arrow animation*/
@keyframes fade_move_down {
    0%   { transform:translate(0,-15px) rotate(45deg); opacity: 0;  }
    25%  {opacity: 1;}
    /* 50%  { opacity: 1;  } */
    100% { transform:translate(0,10px) rotate(45deg); opacity: 0; }
  }

.container_page_2 {
    width: 100%;
    height: 100vh;
    scroll-snap-align: center;
    overflow: hidden;
    position: relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>


<div id="parent-container">
        <div id="child-container">

            <!-- #header -->
            <header>
                <div class="nav-container">
                    <ul>
                        <li></li>
                        <li></li>
                        <li></li>
                    </ul>
                </div>
                <a href="#page2"><h1 id="sticky-title">Lorem ipsum</h1></a>
                <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Modi debitis in libero tenetur suscipit iusto eum nulla dolorum aperiam adipisci unde veritatis vel iure, a nam, saepe exercitationem illum vitae.</p>
                <div class="scroll-down-arrow"></div>
            </header>

            <!-- #page2 -->
            <div id="page2" class="container_page_2">
                <div class="column active">
                    <div class="content">
                        <h1>1</h1>
                        <div class="box">
                            <h2>background-attachment: fixed;</h2>
                            <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Inventore necessitatibus possimus fuga voluptate incidunt enim eius sed, ad suscipit error quasi ex blanditiis ipsa, at vero officiis voluptatem a modi!                                
                            </p>
                        </div>
                    </div>
                    <div class="bg bg1"></div>
                </div>
                <div class="column">
                    <div class="content">
                        <h1>2</h1>
                        <div class="box">
                            <h2>background-attachment: scroll;</h2>
                            <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Inventore necessitatibus possimus fuga voluptate incidunt enim eius sed, ad suscipit error quasi ex blanditiis ipsa, at vero officiis voluptatem a modi!                                
                            </p>
                        </div>
                    </div>
                    <div class="bg bg2"></div>
                </div>
                <div class="column">
                    <div class="content">
                        <h1>3</h1>
                        <div class="box">
                            <h2>background-attachment: scroll;</h2>
                            <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Inventore necessitatibus possimus fuga voluptate incidunt enim eius sed, ad suscipit error quasi ex blanditiis ipsa, at vero officiis voluptatem a modi!                                
                            </p>
                        </div>
                    </div>
                    <div class="bg bg3"></div>
                </div>
                <div class="column">
                    <div class="content">
                        <h1>4</h1>
                        <div class="box">
                            <h2>background-attachment: fixed;</h2>
                            <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Inventore necessitatibus possimus fuga voluptate incidunt enim eius sed, ad suscipit error quasi ex blanditiis ipsa, at vero officiis voluptatem a modi!                                
                            </p>
                        </div>
                    </div>
                    <div class="bg bg4"></div>
                </div>
            </div>

https://codepen.io/bleah1/pen/gjYBgQ

我还没有添加第二页中的所有元素,但这没关系,因为滚动不会受到影响。如您所见,它一点都不平滑,实际上非常活泼。

您怎么看?我想保持滚动快照,因为我喜欢这个主意。

2 个答案:

答案 0 :(得分:2)

您好,您可以尝试此解决方案。 基本上,我在单击事件开始时删除了css,然后在scrollTop事件结束时添加了它。 请记住将其从CSS #child-container

中删除
$(".scroll-down-arrow").click(function() {
  $('#child-container').css('scroll-snap-type','')
    $('html,body,#child-container').animate({
       scrollTop: $("#page2").offset().top}, 'slow', 'linear')
      .promise()
      .done(() => {$('#child-container')
      .css('scroll-snap-type','both proximity')
    });
});

答案 1 :(得分:0)

基于@Ekin Alcar answer,我能够解决我的问题。我遵循他的想法,通过在原始脚本中使用scroll-snap-type#child-container中删除$('#child-container').css('scroll-snap-type',''); css属性,如下所示:

$(".scroll-down-arrow-container").click(function() {
    $('#child-container').css('scroll-snap-type','');
    $('html, body, #child-container').animate({
            scrollTop: $(window).height()
        }, 1000)
    .promise()
    .done(() => {$('#child-container')
        .css('scroll-snap-type','both proximity')
    });
});

使用.css的技巧在于,它只能删除style文件内.html标记中使用的属性。它不适用于.css样式表。

API's documentation

  

但是,它不会删除样式表或