[英]How can I make my function to run the moment the page is running?
I would like to make a function run the moment a person loads my page. 我想让一个函数在有人加载我的页面时运行。 No interactivity. 没有互动。 The function is loading when someone clicks. 当有人单击时,该功能正在加载。 I also would like to make the function loop after 1 minute and a half. 我还想让函数在1分半钟后循环。 I tried replacing "click" for window.onload and body.onload but as you may have noticed by now I am a true begginer with javascript. 我尝试将window.onload和body.onload替换为“ click”,但您可能已经注意到,我现在是javascript的真正开始。 Can someone give me a hand please. 有人可以帮我吗。 For now all I have is this: 现在我所拥有的是:
// Animate an element by adding a class to it: // Paramaters: // anim: the class name to add // time: animation duration (optional, fallsback to the class) // cb: an optional callback function to happen once the animation ends $.fn.animatecss = function(anim, time, cb) { if (time) this.css('-webkit-transition', time / 1000 + 's'); this.addClass(anim); if ($.isFunction(cb)) { setTimeout(function() { // Ensure that the element is available inside the callback. $(this).each(cb); }, (time) ? time : 5000); } return this; }; $(document).ready(function() { $('.box')click(function() { $(this).animatecss('blur-out', 5000, function() { console.log('callback'); }); }); });
.blur-out { -webkit-filter: blur(8px); -webkit-transform: scale(1.4, 1.4); -webkit-transition: all 5s ease-out; transition: all 5s ease-out; visibility: hidden; } .box { background:#fff; margin: 80px; padding: 20px; }
<div class="box"> <img src="http://companionplants.com/images/small-plant2.jpg"> </div>
$(document).ready(function() {
$('.box').click(function() {
$(this).animatecss('blur-out', 5000, function() {
console.log('callback');
});
});
// add this line
$('.box').trigger('click');
});
Reference: http://api.jquery.com/trigger/ 参考: http : //api.jquery.com/trigger/
If I understood correctly, You want to start animation automatically after page is loaded. 如果我理解正确,则要在页面加载后自动开始动画。
Remove event handler for click. 删除点击事件处理程序。 Leave code for animation like this: 保留动画代码,如下所示:
$(document).ready(function() { $('.box').animatecss('blur-out', 5000, function() { console.log('callback'); }); });
For automatically repeating this code, you need to place this code in recursive function with settimeout(). 为了自动重复此代码,您需要使用settimeout()将此代码放在递归函数中。
$(document).ready(function() { function animate(){ $('.box').animatecss('blur-out', 5000, function() { console.log('callback'); }); setTimeout( animate(), 60000); } animate(); });
I hope that there are no errors, I cann't check it now, but I think it will help you 希望没有错误,现在无法检查,但我认为它将为您提供帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.