简体   繁体   English

使用jQuery在页面之间淡入淡出

[英]Fade between pages using jquery

I'm trying to add a transition effect to my site with Jquery. 我正在尝试使用Jquery向我的网站添加过渡效果。 Everything was working, but suddenly the site started flickering one time before the fade in effect... I add a display:none and it was solved. 一切正常,但是突然之间,该站点开始闪烁一次,直到渐隐效果消失了……我添加了display:none并解决了问题。 But if visitors have JavaScript disabled, they won't be able to view the site. 但是,如果访问者禁用了JavaScript,则将无法查看该网站。 I've already add it as a script but it doesn't work... Any ideas? 我已经将其添加为脚本,但是它不起作用...有什么想法吗? Here is my fade code: 这是我的淡入淡出代码:

<!--transition-->
    <script type="text/javascript">
        $(document).ready(function() {

            $("body").css("display", "none");
            $("img").css("display", "none");

            $("body").fadeIn(2000);
            $("img").fadeIn(2000);

            $("a.link").click(function(event){
            event.preventDefault();
            linkLocation = this.href;
            $("body").fadeOut(1000, redirectPage);
            $("img").fadeOut(1000, redirectPage);   
        });

        function redirectPage() {
            window.location = linkLocation;
        }

        });
    </script>

You can perform the fade in transition using CSS only, like so: 您只能使用CSS执行渐变过渡,如下所示:

body {
    animation: myfadeInAnimation 2s;
}
div {
    width: 300px;
}
@keyframe myfadeInAnimation {
    from {opacity: 0;}
    to {opacity: 1;}
}
@-webkit-keyframes myfadeInAnimation {
    from {opacity: 0;}
    to {opacity: 1;}
}

Here is the JSFiddle demo 这是JSFiddle演示

As for the fade out effect, if the browser will not be running javascript, you will have no means of detecting the window unload and trigger an effect for it... 至于淡出效果,如果浏览器不运行javascript,您将无法检测到窗口卸载并为其触发效果...

For javascript disabled : You can add this to your page,so user wont be able to see anything rather than a blank page and a useful message 对于禁用了javascript的用户 :可以将其添加到页面中,因此用户将只能看到空白页面和有用的消息,而看不到其他任何内容

<noscript>
         Javascript is not enabled on browser and require this to be enabled to function properly.
         Here are the <a href="http://www.enable-javascript.com/" target="_blank">
         instructions how to enable JavaScript in your web browser</a>.
         <!-- <meta http-equiv="refresh" content="0;url=http://www.enable-javascript.com/">-->
          <style type="text/css">
             div.container { display:none;}
          </style>
 </noscript>

assuming every thing is inside your container 假设所有东西都在容器内

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

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