2016-08-21 77 views
-5
$(selector).live(events, data, handler); 

如何让它在页面加载后执行动画?目前是如何使用jQuery的.live()在加载后为页面设置动画效果

$('a[data-reveal-id]').live('click',function(e) { 

但我希望它做页面加载后的动画,而不点击任何东西。

我试图改变'点击'到'加载',但看起来不工作。

$('a[data-reveal-id]').live('load',function(e) { 

原始代码:

(function($) { 

/--------------------------- 监听数据-reveal-ID属性 ----------------------------/

$('a[data-reveal-id]').live('click',function(e) { 
    e.preventDefault(); 
    var modalLocation = $(this).attr('data-reveal-id'); 
    $('#'+modalLocation).reveal($(this).data()); 
}); 

/----- ---------------------- 延伸并执行 ----------------------------/

$.fn.reveal = function(options) { 


    var defaults = { 
     animation: 'fadeAndPop', //fade, fadeAndPop, none 
     animationspeed: 300, //how fast animtions are 
     closeonbackgroundclick: true, //if you click background will modal close? 
     dismissmodalclass: 'close-reveal-modal' //the class of a button or element that will close an open modal 
    }; 

    //Extend dem' options 
    var options = $.extend({}, defaults, options); 

    return this.each(function() { 

/---------- ----------------- 全局变量 ----------------------------/

 var modal = $(this), 
      topMeasure = parseInt(modal.css('top')), 
      topOffset = modal.height() + topMeasure, 
      locked = false, 
      modalBG = $('.reveal-modal-bg'); 

/--------------------------- 创建模态BG -------- --------------------/

 if(modalBG.length == 0) { 
      modalBG = $('<div class="reveal-modal-bg" />').insertAfter(modal); 
     }   

/--------------------------- 打开&关闭动画 ---------- ------------------/

 //Entrance Animations 
     modal.bind('reveal:open', function() { 
      modalBG.unbind('click.modalEvent'); 
      $('.' + options.dismissmodalclass).unbind('click.modalEvent'); 
      if(!locked) { 
       lockModal(); 
       if(options.animation == "fadeAndPop") { 
        modal.css({'top': $(document).scrollTop()-topOffset, 'opacity' : 0, 'visibility' : 'visible'}); 
        modalBG.fadeIn(options.animationspeed/2); 
        modal.delay(options.animationspeed/2).animate({ 
         "top": $(document).scrollTop()+topMeasure + 'px', 
         "opacity" : 1 
        }, options.animationspeed,unlockModal());     
       } 
       if(options.animation == "fade") { 
        modal.css({'opacity' : 0, 'visibility' : 'visible', 'top': $(document).scrollTop()+topMeasure}); 
        modalBG.fadeIn(options.animationspeed/2); 
        modal.delay(options.animationspeed/2).animate({ 
         "opacity" : 1 
        }, options.animationspeed,unlockModal());     
       } 
       if(options.animation == "none") { 
        modal.css({'visibility' : 'visible', 'top':$(document).scrollTop()+topMeasure}); 
        modalBG.css({"display":"block"}); 
        unlockModal()    
       } 
      } 
      modal.unbind('reveal:open'); 
     }); 

     //Closing Animation 
     modal.bind('reveal:close', function() { 
      if(!locked) { 
       lockModal(); 
       if(options.animation == "fadeAndPop") { 
        modalBG.delay(options.animationspeed).fadeOut(options.animationspeed); 
        modal.animate({ 
         "top": $(document).scrollTop()-topOffset + 'px', 
         "opacity" : 0 
        }, options.animationspeed/2, function() { 
         modal.css({'top':topMeasure, 'opacity' : 1, 'visibility' : 'hidden'}); 
         unlockModal(); 
        });     
       } 
       if(options.animation == "fade") { 
        modalBG.delay(options.animationspeed).fadeOut(options.animationspeed); 
        modal.animate({ 
         "opacity" : 0 
        }, options.animationspeed, function() { 
         modal.css({'opacity' : 1, 'visibility' : 'hidden', 'top' : topMeasure}); 
         unlockModal(); 
        });     
       } 
       if(options.animation == "none") { 
        modal.css({'visibility' : 'hidden', 'top' : topMeasure}); 
        modalBG.css({'display' : 'none'}); 
       }  
      } 
      modal.unbind('reveal:close'); 
     });  

/--------------------- ------ 打开和关闭添加监听 ----------------------------/

 //Open Modal Immediately 
    modal.trigger('reveal:open') 

     //Close Modal Listeners 
     var closeButton = $('.' + options.dismissmodalclass).bind('click.modalEvent', function() { 
      modal.trigger('reveal:close') 
     }); 

     if(options.closeonbackgroundclick) { 
      modalBG.css({"cursor":"pointer"}) 
      modalBG.bind('click.modalEvent', function() { 
       modal.trigger('reveal:close') 
      }); 
     } 
     $('body').keyup(function(e) { 
      if(e.which===27){ modal.trigger('reveal:close'); } // 27 is the keycode for the Escape key 
     }); 

/--------------------------- Animations Locks ---------------- ------------/

 function unlockModal() { 
      locked = false; 
     } 
     function lockModal() { 
      locked = true; 
     } 

    });//each call 
}//orbit plugin call 
})(jQuery); 

HTML:

<a href="#" data-reveal-id="myModal" id="first_load"></a> 

    <div id="myModal" class="reveal-modal reveal-modal-1"> 
     <a class="close-reveal-modal hvr-push"></a> 
     <div id="dotype"> 
      <div class="type-wrap"> 
       <div id="typed-strings"> 
       <p>Hey there, I lost my ingredients<br> yesterday at the city. I was going <br> to make you a birthday cake.<br> Can you help me to find it?</p> 
       </div> 
      <span id="typed"></span> 
      </div> 
     </div> 
    </div> 
+1

这个称号不太可能让任何人在你的问题上得到很好的处理。 – jonrsharpe

+1

'.live()'已弃用,请勿使用它。 –

+0

感谢您编辑标题。 –

回答

1

你不应该使用live,因为它已经过时了。为了使事件发生时,该文件已加载,你可以使用下列事件:

$(document).ready(function() { 

}); 

完整的示例:

$(document).ready(function() { 
 
    $("div").animate({top: "200px"}, 4000); 
 
});
div { 
 
    border: 1pt solid blue; 
 
    position: relative; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 
 

 

 
<div> 
 
    Animate on load example 
 
</div>

+0

我累了它不适合我。原来是这个样子\t $( '一[数据显示-ID]')生活( '点击',功能(E){ \t \t e.preventDefault(); \t \t VAR modalLocation = $(本).attr('data-reveal-id'); \t \t $('#'+ modalLocation).reveal($(this).data()); \t}); –

+0

@AndersonKoh - 我提供了一个完整的例子,看看这是否有帮助。 –

+0

看起来很酷,但当我的页面加载完成后,它不会淡入淡出。我会告诉你我的完整编码吗? –

0

就试试这个:

$(window).load(function() { 
    // your code 
}); 
0

好吧,我加入这个

$(document).ready(function(){ 
    $('a[data-reveal-id]').delay(500).trigger('click'); 
}); 

$('a[data-reveal-id]').live('click',function(e) { 
    e.preventDefault(); 
    var modalLocation = $(this).attr('data-reveal-id'); 
    $('#'+modalLocation).reveal($(this).data()); 
}); 

谢谢。我解决了我自己的问题。

我也这样做的延迟。

$(document).ready(function(){ 
    setTimeout(function(){ 
     $('a[data-reveal-id]').delay(500).trigger('click'); 
    }, 800); 
}); 
相关问题