简体   繁体   English

jQuery-缓慢加载页面并加载gif效果

[英]Jquery - load page with slowly and with loading gif effect

I'm trying to make my page loads with loading effect. 我正在尝试使页面加载具有加载效果。

A small example about what im talking: 关于即时通讯的一个小例子:

<a href="#" id="a1">PAGE1</a>
<a href="#" id="a2">PAGE2</a>
<a href="#" id="a3">PAGE3</a>

<div id="page1">Hey this is page 1</div>
<div id="page2">Hey this is page 2</div>
<div id="page3">Hey this is page 3</div>

Jquery : jQuery的:

$(document).ready(function(){

   $("#page1").hide();
   $("#page2").hide();
   $("#page3").hide();

    $("#a1").click(function(){
        $("#page1").show();
        return false;
    });

    $("#a2").click(function(){
        $("#page2").show();
        return false;
    });

    $("#a3").click(function(){
        $("#page3").show();
        return false;
    });

});

Hope you understand what im talking about, and is it posible to make it slower for load? 希望您了解我在说什么,是否有可能使其加载速度变慢? I mean smooth. 我是说顺利。

 $(document).ready(function() { $("#page1").hide(); $("#page2").hide(); $("#page3").hide(); $("#loadingGif").hide(); function anim(id) { $("#loadingGif").show(); setTimeout(function() { $("#loadingGif").hide(); $(id).fadeIn("slow"); }, 400) } $("#a1").click(function() { anim("#page1"); return false; }); $("#a2").click(function() { anim("#page2"); return false; }); $("#a3").click(function() { anim("#page3"); return false; }); }); 
 #loadingGif { width: 200px; height: 200px; position: fixed; top: 100px; left: 100px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#" id="a1">PAGE1</a> <a href="#" id="a2">PAGE2</a> <a href="#" id="a3">PAGE3</a> <div id="page1">Hey this is page 1</div> <div id="page2">Hey this is page 2</div> <div id="page3">Hey this is page 3</div> <img id="loadingGif" src="http://blog.teamtreehouse.com/wp-content/uploads/2015/05/InternetSlowdown_Day.gif" width="200" height="200" /> 

You can use Jquery's fadeIn 您可以使用jQuery的fadeIn

$(selector).fadeIn(speed,callback); $(选择器).fadeIn(速度,回调);

You can read more at http://www.w3schools.com/jquery/jquery_fade.asp 您可以在http://www.w3schools.com/jquery/jquery_fade.asp阅读更多信息

and here http://api.jquery.com/fadein/ 和这里http://api.jquery.com/fadein/

Because you're using jQuery you could use .fadeToggle() instead of show() check example below. 因为您使用的是jQuery,所以可以使用.fadeToggle()代替下面的show()检查示例。

NOTES : 注意事项:

  • It's better if you could use one event instead of repeating the same code for every new link, if you don't want to toggle between show/hide of the page you could use fadeIn() . 最好使用一个事件,而不是对每个新链接重复相同的代码,如果您不想在页面的show/hide之间进行切换,则可以使用fadeIn()

  • You could use comma separator to trigger the same method on multiple selectors : 您可以使用逗号分隔符在多个选择器上触发相同的方法:

     $("#page1,#page2,#page3").hide(); 

Hope this helps. 希望这可以帮助。


 $(function(){ $("#page1,#page2,#page3,#loading").hide(); $("a").click(function(e){ e.preventDefault(); $('#loading').show(); switch($(this).attr('id')){ case 'a1': $("#page1").fadeToggle("slow",function(){ $('#loading').hide() }); break; case 'a2': $("#page2").fadeToggle(2000,function(){ $('#loading').hide() }); break; case 'a3': $("#page3").fadeToggle("fast",function(){ $('#loading').hide() }); break; } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <img id="loading" src="http://www.tbaf.org.tw/event/2016safe/imgs/loader1.gif" width="250"> <a href="#" id="a1">PAGE1</a> <a href="#" id="a2">PAGE2</a> <a href="#" id="a3">PAGE3</a> <div id="page1">Hey this is page 1</div> <div id="page2">Hey this is page 2</div> <div id="page3">Hey this is page 3</div> 

You can create one div called preloader and when you click on link you can use fadeIn() then you load your content and then you fadeOut preloader with slight delay 您可以创建一个称为preloader div,当您单击链接时可以使用fadeIn()然后加载内容,然后稍微delay一下fadeOut preloader

 var preloader = $('.preloader'); preloader.hide(); $('a').click(function() { var target = $(this).data('target'); preloader.fadeIn(function() { $(target).siblings().css('opacity', 0); $(target).css('opacity', 1); }).delay(1500).fadeOut(); }); 
 .preloader { position: fixed; width: 100vw; height: 100vh; left: 0; top: 0; background: white; display: flex; align-items: center; justify-content: center; } .content { opacity: 0; transition: all 0.4s ease-in; position: absolute; top: 50px; left: 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#" data-target="#page1">PAGE1</a> <a href="#" data-target="#page2">PAGE2</a> <a href="#" data-target="#page3">PAGE3</a> <div class="content-container"> <div class="content" id="page1">Hey this is page 1</div> <div class="content" id="page2">Hey this is page 2</div> <div class="content" id="page3">Hey this is page 3</div> </div> <div class="preloader"> <img src="https://d13yacurqjgara.cloudfront.net/users/12755/screenshots/1037374/hex-loader2.gif" alt=""> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM