如何使DIV振动或嗡嗡作响?(类似于iOS的移动/删除应用程序功能)
How do I make a DIV vibrate or buzz? (similar to the iOS move/delete app feature)
我正在开发一个用户可以自定义的网站,我想让"vibrate/buzz"成为DOM的一个元素(在我的特定情况下,它是DIV
(。我想获得类似于iOS上长按任何应用程序图标时发生的效果(所有图标都会抖动(。
在网上搜索我刚刚发现了这个jQuery库:http://www.dev4web.eu/projects/jquery.vibrate/
但我不确定我是否能够使用它获得良好的效果
关于如何实现这一点,有什么想法吗?
谢谢!
您也可以实现自己的动画,如下所示:
function shake(){
$('#div').animate({
'margin-left': '-=5px',
'margin-right': '+=5px'
}, 200, function() {
$('#div').animate({
'margin-left': '+=5px',
'margin-right': '-=5px'
}, 200, function() {
//and so on...
});
});
}
这里有一些Javascript可以完成您想要的任务。
var times = 10;
var duration = 200;
for (var i = 0; i < times; i++)
$('div#shake-it').animate({
left: (i % 2 === 0 ? "-" : "+") + "=50"
}, duration);
与许多其他解决方案一样,它需要jQuery库。然而,它不需要任何其他插件。
我最近在jQuery sortable之上构建了一些旨在模仿iOS摆动效果的东西。你可以在tastemakerx.com上看到它的直播,我用它来增强收藏排序功能。
这是我开始用的小提琴:http://jsfiddle.net/jozecuervo/fv8vR/
这是CSS:
@-webkit-keyframes shake {
0%, 100% {-webkit-transform: translateX(0) rotate(0deg) translateY(0);}
15%, 35%, 55%, 75%, 95% {-webkit-transform: translateX(-1px) rotate(-2deg) ;}
25%, 45%, 65%, 85% {-webkit-transform: translateX(1px) rotate(2deg); }
10%, 30%, 50%, 70%, 90% {-webkit-transform: translateY(1px);}
20%, 40%, 60%, 80% {-webkit-transform: translateY(-1px); }
}
@-moz-keyframes shake {
0%, 100% {-moz-transform: translateX(0) rotate(0deg) translateY(0);}
15%, 35%, 55%, 75%, 95% {-moz-transform: translateX(-1px) rotate(-2deg) ;}
25%, 45%, 65%, 85% {-moz-transform: translateX(1px) rotate(2deg); }
10%, 30%, 50%, 70%, 90% {-moz-transform: translateY(1px);}
20%, 40%, 60%, 80% {-moz-transform: translateY(-1px); }
}
@-o-keyframes shake {
0%, 100% {-o-transform: translateX(0) rotate(0deg) translateY(0);}
15%, 35%, 55%, 75%, 95% {-o-transform: translateX(-1px) rotate(-2deg) ;}
25%, 45%, 65%, 85% {-o-transform: translateX(1px) rotate(2deg); }
10%, 30%, 50%, 70%, 90% {-o-transform: translateY(1px);}
20%, 40%, 60%, 80% {-o-transform: translateY(-1px); }
}
@keyframes shake {
0%, 100% {transform: translateX(0) rotate(0deg) translateY(0);}
15%, 35%, 55%, 75%, 95% {transform: translateX(-1px) rotate(-2deg) ;}
25%, 45%, 65%, 85% {transform: translateX(1px) rotate(2deg); }
10%, 30%, 50%, 70%, 90% {transform: translateY(1px);}
20%, 40%, 60%, 80% {transform: translateY(-1px); }
}
.shake {
-webkit-animation-name: shake;
-moz-animation-name: shake;
-o-animation-name: shake;
animation-name: shake;
-webkit-animation-duration: 2s;
-moz-animation-duration: 2s;
-o-animation-duration: 2s;
animation-duration: 2s;
-webkit-animation-fill-mode: both;
-moz-animation-fill-mode: both;
-o-animation-fill-mode: both;
animation-fill-mode: both;
-webkit-animation-iteration-count: infinite;
-moz-animation-iteration-count: infinite;
-o-animation-iteration-count: infinite;
animation-iteration-count: infinite;
-webkit-transition-timing-function:linear;
}
使用jquery的插件:
$('#loginL').effect('shake');
"jQuery ClassyWiggle允许您模拟iPhone上按住图标时的摆动效果。">
查看Marius Stanciu的Classy Wiggle JQuery插件。
您可以使用jQuery
Rumble这样的插件:http://jackrugile.com/jrumble/
您可以使用animate
创建自己的。举个例子(演示如何摇动div(:
<div id="div">
</div>
<input type="button" id="buzz" />
$("#buzz").click(function() {
$("#div").animate({
left: "20px",
}, 50);
$("#div").animate({
left: "-20px",
}, 50);
$("#div").animate({
left: "20px",
},50);
});
http://jsfiddle.net/VRzc9/1/
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
$('#shakediv').effect('shake');
抖动效果是在jqueryui中构建的,导入并使用
遵循以下示例
<div class="buzz">
<!-- Your Div Content Here -->
</div>
<script>
$(docuement).ready(function(){
buzzMe();
//or
buzzMeFor(0)
})
function buzzMe()
{
$('buzz').css('margin-left','-2');
delay(500);
$('buzz').css('margin-left','2');
delay(500);
buzzMe()
}
//Or use this function for Specific times
//Call & Pass Arguments 0
function buzzMeFor(count)
{
count++;
$('buzz').css('margin-left','-2');
delay(500);
$('buzz').css('margin-left','2');
delay(500);
if(count!=20)
buzzMe(count)
}
</script>
只使用jQuery的animate函数,以下操作就完成了:
function recursiveShake(jQelement, pixel, counter) {
if (counter > 0) {
jQelement.animate({
'margin-left': '+='.concat(pixel, 'px')},
30,
recursiveShake(jQelement, -pixel, counter - 1)
);
}
}
// (counter must be even to avoid any shift)
- 检测iOS移动设备的JavaScript比自动播放音频
- 删除的DOM元素仍然显示在移动safari(iOS 5.1)中
- 使用AngularJS应用程序为移动设备(iOS、Android等)构建本地应用程序的最佳方式
- 在移动设备(ios)上打印Javascript
- 视频未在任何具有ios和Android版本的移动设备上播放,但在网页上播放
- JS点击移动替代品 - 在iOS和Android上也弃用了onclick
- HTML5 部分触摸开始触摸移动触摸触摸 iOS
- HTML & JS:如何在移动设备(iOS)上打开带有视口的网站,默认情况下使用小视口
- iOS 移动 Safari 服务工作进程缓存限制
- 监控移动野生动物园,iOS 6和7上的实时滚动位置
- 使用后退按钮时如何防止Safari ios移动缓存
- Opacity获胜'在iOS设备上滚动或触摸移动事件时,不要设置动画
- 用于打包移动应用程序(Android、iOS)的Javascript错误日志记录/报告/跟踪器
- 可以在移动设备(iOS/Android)的后台观看位置
- 如何使用AngularJS创建移动应用程序(Android,IOS,Blockberry)
- Javascript:在移动设备上使用DOMParser(iOS 7.*)
- iOS上陀螺仪移动和触摸移动的JavaScript存在冲突,当第二部分正在使用时,我如何暂停其中一部分
- Jquery移动iOS -动量滚动防止点击处理程序被调用
- 嵌入Youtube视频不能在移动iOS Safari中播放
- 谷歌地图MarkerImage与本地url不显示在Safari移动iOS 6