失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > jquery滚动条平滑滚动_使用jQuery平滑垂直或水平页面滚动

jquery滚动条平滑滚动_使用jQuery平滑垂直或水平页面滚动

时间:2022-02-20 08:52:15

相关推荐

jquery滚动条平滑滚动_使用jQuery平滑垂直或水平页面滚动

jquery滚动条平滑滚动

View demo 查看演示Download Source 下载源

In this tutorial we will create a simple smooth scrolling effect with jQuery. We will create a horizontal and a vertical website layout to show the effect. We will be using the jQuery Easing Plugin and just a few lines of jQuery.

在本教程中,我们将使用jQuery创建简单的平滑滚动效果。 我们将创建水平和垂直网站布局以显示效果。 我们将使用jQuery简易插件和几行jQuery。

So, let’s start!

所以,让我们开始吧!

标记 (The Markup)

The markup for our example page is going to be very plain. We will have three sections with a heading and a paragraph and am unordered list for the navigation:

我们的示例页面的标记将非常简单。 我们将分为三个部分,分别包含标题和段落,以及导航的无序列表:

<div class="section black" id="section1"><h2>Section 1</h2><p>MY Spectre around me night and dayLike a wild beast guards my way;My Emanation far withinWeeps incessantly for my sin.</p><ul class="nav"><li>1</li><li><a href="#section2">2</a></li><li><a href="#section3">3</a></li></ul></div><div class="section white" id="section2"><h2>Section 2</h2><p>A fathomless and boundless deep,There we wander, there we weep;On the hungry craving windMy Spectre follows thee behind.</p><ul class="nav"><li><a href="#section1">1</a></li><li>2</li><li><a href="#section3">3</a></li></ul></div><div class="section black" id="section3"><h2>Section 3</h2><p>He scents thy footsteps in the snowWheresoever thou dost go,Thro' the wintry hail and rain.When wilt thou return again?</p><ul class="nav"><li><a href="#section1">1</a></li><li><a href="#section2">2</a></li><li>3</li></ul></div>

The HTML is going to be the same for both examples. Let’s take a look at the style.

两个示例HTML都将相同。 让我们看一下样式。

CSS (The CSS)

Since we have two examples,we will start with the horizontal page style.

由于有两个示例,因此我们将从水平页面样式开始

The main idea is to make the sections very wide and 100% in height. We will add a background image to the bottom right of each section:

主要思想是使这些部分非常宽且高度为100%。 我们将在每个部分的右下方添加背景图片:

*{margin:0;padding:0;}body{background:#000;font-family:Georgia;font-size: 34px;font-style: italic;letter-spacing:-1px;width:12000px;position:absolute;top:0px;left:0px;bottom:0px;}.section{margin:0px;bottom:0px;width:4000px;float:left;height:100%;text-shadow:1px 1px 2px #f0f0f0;}.section h2{margin:50px 0px 30px 50px;}.section p{margin:20px 0px 0px 50px;width:600px;}.black{color:#fff;background:#000 url(../images/black.jpg) no-repeat top right;}.white{color:#000;background:#fff url(../images/white.jpg) no-repeat top right;}.section ul{list-style:none;margin:20px 0px 0px 550px;}.black ul li{float:left;padding:5px;margin:5px;color:#aaa;}.black ul li a{display:block;color:#f0f0f0;}.black ul li a:hover{text-decoration:none;color:#fff;}.white ul li{float:left;padding:5px;margin:5px;color:#aaa;}.white ul li a{display:block;color:#222;}.white ul li a:hover{text-decoration:none;color:#000;}

We need to give the body a valid height, because we want to be able to define the height 100% to the section. With positioning the body absolutely and saying top:0px and bottom:0px we stretch the body and give it a height.

我们需要给主体一个有效的高度,因为我们希望能够将截面的高度定义为100%。 通过绝对定位身体并说出top:0px和bottom:0px,我们拉伸了身体并赋予其高度。

The style for the vertical page layout is going to look as follows:

垂直页面布局的样式如下所示:

*{margin:0;padding:0;}body{background:#000;font-family:Georgia;font-size: 34px;font-style: italic;letter-spacing:-1px;}.section{margin:0px;height:4000px;width:100%;float:left;text-shadow:1px 1px 2px #f0f0f0;}.section h2{margin:50px 0px 30px 50px;}.section p{margin:20px 0px 0px 50px;width:600px;}.black{color:#fff;background:#000 url(../images/black_vert.jpg) repeat-x bottom left;}.white{color:#000;background:#fff url(../images/white_vert.jpg) repeat-x bottom left;}.section ul{list-style:none;margin:20px 0px 0px 550px;}.black ul li{float:left;padding:5px;margin:5px;color:#aaa;}.black ul li a{display:block;color:#f0f0f0;}.black ul li a:hover{text-decoration:none;color:#fff;}.white ul li{float:left;padding:5px;margin:5px;color:#aaa;}.white ul li a{display:block;color:#222;}.white ul li a:hover{text-decoration:none;color:#000;}

Nothing special here, just that we give a big height to the sections. The background image is positioned to the bottom left then.

这里没有什么特别的,只是我们为这些部分提供了很大的高度。 然后,背景图像位于左下方。

Let’s add the JavaScript

让我们添加JavaScript

JavaScript (The JavaScript)

The function for thehorizontal scrolling effectis the following:

水平滚动效果的功能如下:

$(function() {$('ul.nav a').bind('click',function(event){var $anchor = $(this);/*if you want to use one of the easing effects:$('html, body').stop().animate({scrollLeft: $($anchor.attr('href')).offset().left}, 1500,'easeInOutExpo');*/$('html, body').stop().animate({scrollLeft: $($anchor.attr('href')).offset().left}, 1000);event.preventDefault();});});

And the function for thevertical scrolling effectis the following:

垂直滚动效果的功能如下:

$(function() {$('ul.nav a').bind('click',function(event){var $anchor = $(this);$('html, body').stop().animate({scrollTop: $($anchor.attr('href')).offset().top}, 1500,'easeInOutExpo');/*if you don't want to use the easing effects:$('html, body').stop().animate({scrollTop: $($anchor.attr('href')).offset().top}, 1000);*/event.preventDefault();});});

You can animate to the respective element by using one of the easing effects. You can see the effect in the vertical demo. If there is no JavaScript enabled, we still have our good old scroll bars.

您可以使用缓动效果之一为各个元素设置动画。 您可以在垂直演示中看到效果。 如果没有启用JavaScript,我们仍然会保留旧的滚动条。

Check out the demo, it will lead you to the horizontal page. Here is the direct link to the demo of the vertical page scrolling, or simply click on the link in the horizontal demo.

查看演示,它将引导您进入横向页面。 这是垂直页面滚动演示的直接链接,或者直接单击水平演示中的链接。

testking E20-322 design guide and testking E20-322设计指南和testking 642-533 live demos, learn how to create effective web applications and themes for your web page. Improve your website accessibility with testking 642-533实时演示,了解如何为您的网页创建有效的Web应用程序和主题。 使用testking 70-620 web designing course.testking 70-620网页设计课程来改善您的网站可访问性。

翻译自: /codrops//06/02/smooth-vertical-or-horizontal-page-scrolling-with-jquery/

jquery滚动条平滑滚动

如果觉得《jquery滚动条平滑滚动_使用jQuery平滑垂直或水平页面滚动》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。