简体   繁体   English

如何使我的函数在页面运行时立即运行?

[英]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.

相关问题 如何使函数在html页面中自动运行? - How can I make a function run automatically in html page? 我该如何立即制作这种特定格式 - how can I make this specific format with moment 如何使 function 仅在使用 angular 重新加载页面时运行? - How can I make a function run only when I reload the page with angular? 如何使用 static 变量制作 js function? 我如何为页面中的每个图像运行这些功能? - How to make js function with static variables ? How can i run these functions for every image in the page? 当页面加载时,如何使用特定的javascript函数使URL运行 - How can I make a url with speciphic javascript function to run when the page is load 如何在更改值而不是单击按钮时运行函数? - How can I make my function run when a value is changed instead of when the button is clicked? Javascript:如何检查fct是否正在运行,如果没有,则使其运行? - Javascript: How can I check if a fct is running and, if not, make it run? 当加载特定页面时,如何在我的 Service Worker 上运行特定功能? - How can I run a certain function on my Service Worker when a specific page is loaded? 每当页面加载时,如何使插件运行函数 - How do I make an addon run a function whenever a page is loaded 我必须制作一个可以运行一些简单JavaScript的网页,但是我认为我的脚本没有运行 - I have to make a webpage that can run some simple JavaScript, but I don't think my script is running
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM