在本文中,我們將通過使用名為script.aculo.us的JavaScript庫(從一個平滑過渡到另一個平滑過渡)來演示ScrollTo的效果。我們也可以調整效果的持續時間。
用法:
Effect.ScrollTo('element_id', { duration:'0.2', offset:-20 });
注意:要使用此庫,我們應該先安裝該庫,然後在程序中使用它。為此,您可以點擊此鏈接http://script.aculo.us/downloads。
方法:為了演示該函數的用法,我們編寫了一小段代碼。我們編寫了一個名為ShowEffect()方法的小型JavaScript函數,該函數使用此庫的Effect.ScrollTo()方法。通過單擊“單擊我滾動”,您將清楚地看到效果。
例:要查看效果,請先下載該庫,然後在本地環境中打開此程序。
HTML
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript"
src="prototype.js">
</script>
<script type="text/javascript"
src="scriptaculous.js">
</script>
<script type="text/javascript">
function ShowEffect(element) {
new Effect.ScrollTo(element,
{ duration:'0.2', offset:-20 });
}
</script>
</head>
<body>
<h2>script.aculo.us Effect.ScrollTo</h2>
<p>
JavaScript is a lightweight, cross-platform
and interpreted scripting language.<br />
It is well-known for the development of web
pages, many non-browser environments also
use it.<br />JavaScript can be used for
Client-side developments as well as
Server-side developments.<br />
</p>
<p>
<b>Features of JavaScript:</b> According to
a recent survey conducted by Stack Overflow,
<br />JavaScript is the most popular language
on earth.<br />With advances in browser
technology and JavaScript having moved into
the server<br />with Node.js and other
frameworks, JavaScript is capable of so much
more.<br /> Here are a few things that we can
do with JavaScript:<br />
</p>
<p>
JavaScript was created in the first place
for DOM manipulation.<br /> Earlier websites
were mostly static, after JS was created
dynamic Web sites were made.<br /> Functions
in JS are objects. They may have properties
and methods just like another object.<br />
They can be passed as arguments in other
functions.<br />Can handle date and time.<br />
Performs Form Validation although the forms are
created using HTML.<br />No compiler needed.<br />
</p>
<p>
JavaScript is a lightweight, cross-platform and
interpreted scripting language.<br />
It is well-known for the development of web pages,
many non-browser environments also use it.<br />
JavaScript can be used for Client-side
developments as well as Server-side
developments.<br />
</p>
<p>
<b>Features of JavaScript:</b> According
to a recent survey conducted by Stack
Overflow,<br /> JavaScript is the most
popular language on earth.<br />
With advances in browser technology and
JavaScript having moved into the server<br />
with Node.js and other frameworks,
JavaScript is capable of so much more.<br />
Here are a few things that we can do
with JavaScript:<br />
</p>
<p>
JavaScript was created in the first place
for DOM manipulation.<br /> Earlier websites
were mostly static, after JS was created
dynamic Web sites were made.<br />
Functions in JS are objects. They may have
properties and methods just like another
object.<br /> They can be passed as arguments
in other functions.<br /> Can handle date
and time.<br />Performs Form Validation
although the forms are created using HTML.<br />
No compiler needed.<br />
</p>
<p>
JavaScript is a lightweight, cross-platform
and interpreted scripting language.<br />
It is well-known for the development of
web pages, many non-browser environments
also use it.<br /> JavaScript can be used
for Client-side developments as
well as Server-side developments.<br />
</p>
<p>
<b>Features of JavaScript:</b> According to a
recent survey conducted by Stack Overflow,<br />
JavaScript is the most popular language on
earth.<br />With advances in browser technology
and JavaScript having moved into the server<br />
with Node.js and other frameworks,JavaScript is
capable of so much more.<br />Here are a few
things that we can do with JavaScript.<br />
</p>
<p>
JavaScript was created in the first place for
DOM manipulation.<br /> Earlier websites were
mostly static,after JS was created dynamic
Web sites were made.<br />Functions in JS are
objects. They may have properties and methods
just like another object.<br />They can be
passed as arguments in other functions.<br />
Can handle date and time.<br />
Performs Form Validation although the forms are
created using HTML.<br />
No compiler needed.<br />
</p>
<p>
JavaScript is a lightweight, cross-platform and
interpreted scripting language.<br />
It is well-known for the development of web pages,
many non-browser environments also use it.<br />
JavaScript can be used for Client-side developments
as well as Server-side developments.<br />
</p>
<a href="#" onclick="ShowEffect('h2'); return false;">
Click me to scroll!
</a>
</body>
</html>
輸出:
相關用法
注:本文由純淨天空篩選整理自Jitender_1998大神的英文原創作品 script.aculo.us ScrollTo Effect。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。