首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >自调用函数调用

自调用函数调用
EN

Stack Overflow用户
提问于 2016-09-14 11:31:26
回答 1查看 83关注 0票数 2

自调用函数调用重复,但函数代码存在重复函数输出的问题。

Javascript

代码语言:javascript
运行
复制
//version 2
 ! function anf1(){
 if(w0 = '0') { //always true will be executed
    $('.fish1').stop().animate({'margin-left': w50, 'margin-top': '30px' }, 25000); //move object to the center of screen
    console.log('start');
}
if(w50 = w50) { //always true will be executed after 1st if condition
    $('.fish1').stop().animate({'margin-left': w70, 'margin-top': '-30px'}, 9000); //move object to 70% of browser width
    console.log('middle')
}
if(w70 = w70) { //always true will be executed after the 2nd if condition
    $('.fish1').stop().animate({'margin-left': ww, 'margin-top': '-45px'}, 35000, function(){
     anf1(); // call self invoked function anf1
     }); //move object to 100% of browser width
    console.log('finish')
}
}();

JSfiddle链路https://jsfiddle.net/qytkg5h2/2/

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-14 11:47:10

使用==进行比较,=用于赋值。在再次调用函数之前,还可以在最终动画中设置原始css。

代码语言:javascript
运行
复制
//window 100%
var ww = $('.container').outerWidth();
//window 0%
var w0 = ww - ww;
//window 50%
var w50 = ww / 2;
//window 70%
var w70 = (70 / 100) * ww;


//version 1
 ! function anf1(){
 if(w0 == '0') { //always true will be executed
    $('.fish1').stop().animate({'margin-left': w50, 'margin-top': '30px' }, 2500); //move object to the center of screen

    console.log('start');
}
if(w50 == w50) { //always true will be executed after 1st if condition
    $('.fish1').stop().animate({'margin-left': w70, 'margin-top': '-30px'}, 900); //move object to 70% of browser width
    console.log('middle')

}
if(w70 == w70) { //always true will be executed after the 2nd if condition
    $('.fish1').stop().animate({'margin-left': ww, 'margin-top': '-45px'}, 3500, function(){
     $(this).css({
  'margin-left': '0px',
  'margin-top': '0px'});
     anf1();
     // call self invoked function anf1
     }); //move object to 100% of browser width
    console.log('finish')

}
}();

JSFIDDLE

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39489354

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档