Jquery UI震動效果實現原理及步驟

來源:互聯網
上載者:User

如果你想你的部落格頁面某些部分引起讀者的注意,你可以使這些部分震動,如廣告,今天這篇文章將介紹怎樣使你的頁面中的元素震動起來。

要達到這個目的我們需要使用到Jquery和Jquery UI。
首先讓我建立一個震動塊,可以是圖片,也可以是普通的dom元素,如div、span等,把元素的id命名為shake,這裡可以任意命名。

我們用圖片如下: 複製代碼 代碼如下:<img src="http://jqueryui.com/jquery-wp-content/themes/jquery/images/logo-jquery-ui.png" id="shake"/>

Jquery UI沒有現成的使元素震動的方法,我們需要藉助於effect方法來實現,文法如下: 複製代碼 代碼如下:effect('shake', options, speed);

參數options(這裡有三個參數):
•times:指定元素震動次數
•distance:指定元素震動幅度
•direction:指定元素震動方向
下面是具體實現方法,設定震動3次,每500ms調用一次震動: 複製代碼 代碼如下:function interval() {
$('#shake').effect('shake', { times:3 }, 100);
}
$(document).ready(function() {
var shake = setInterval(interval, 500);
});

<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js"></script>
這裡我引入了最新版的。
下面附上完整代碼 複製代碼 代碼如下:<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js"></script>
<script>
function interval() {
$('#shake').effect('shake', { times:3 }, 100);
}
$(document).ready(function() {
var shake = setInterval(interval, 500);
});
</script>
<style>
.body{
background: #F9F9F9;
}
h1{
text-align:center;
top:30px;
position: relative;
font-size: 36px;
line-height: 40px;
margin: 0;
position: relative;
font-weight: 300;
color: #C91622;
padding: 5px 0px;
text-shadow: 1px 1px 0px #F2F2F2, 1px 2px 0px #B1B1B2;
font-family: 'KenyanCoffeeRg-Regular';
height:70px;
}
.container{
display:table;
width:50%;
border-collapse: collapse;
margin: 0 auto;
}
.container img {
width:253px;
}
</style>
<title>jQuery Shake Effect</title>
</head>
<body>
<h1>jQuery Shake Effect</h1>
<br/><br/><br/>
<div class="container">
<img src="http://jqueryui.com/jquery-wp-content/themes/jquery/images/logo-jquery-ui.png" id="shake"/>
</div>
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.