最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - fade out on anchor click and fade in href - Stack Overflow

programmeradmin0浏览0评论

I would like to know if it is possible to make fadings between two HTML-Documents. I have a few HTML-Pages but let's make an example with two of them.

index.html, jobs.html

On both I have a menu with <a> buttons. What I want to do is:

I click on <a href="jobs.html" id="jobs">Jobs</a> and index.html (which I am currently on) fades out and jobs.html fades in. Something like fading between divs but with a whole HTML document.

Any helps is much appreciated.

I would like to know if it is possible to make fadings between two HTML-Documents. I have a few HTML-Pages but let's make an example with two of them.

index.html, jobs.html

On both I have a menu with <a> buttons. What I want to do is:

I click on <a href="jobs.html" id="jobs">Jobs</a> and index.html (which I am currently on) fades out and jobs.html fades in. Something like fading between divs but with a whole HTML document.

Any helps is much appreciated.

Share Improve this question asked Aug 2, 2013 at 14:18 supersizesupersize 14.9k19 gold badges85 silver badges144 bronze badges 6
  • 1 are you loading this jobs.html by ajax? – Brad Commented Aug 2, 2013 at 14:20
  • nope! I'm making a page-switch! – supersize Commented Aug 2, 2013 at 14:20
  • 2 This looks cool – musefan Commented Aug 2, 2013 at 14:21
  • @supersize fade the content in on document ready and fade it out when the user leaves the page. If you want for a specific page then filter the url or add classes do body, etc – Spokey Commented Aug 2, 2013 at 14:22
  • @musefan can't use css3! but thanks, I will remember this :) – supersize Commented Aug 2, 2013 at 14:23
 |  Show 1 more ment

4 Answers 4

Reset to default 4
  1. Hide the body using css.
  2. Fade in the body
  3. Click a button and grab its ID
  4. Fade out the body
  5. Navigate to the new url
<!DOCTYPE html>
<html>
<head>
    <style>
        body{
            display: none;
        }
        .myBtn{
            cursor: pointer;
        }
    </style>
    <script type="text/javascript" src="http://ajax.googleapis./ajax/libs/jquery/1.3/jquery.min.js"></script>

    <script>
        $(function(){
            $('body').fadeIn();
            $('.myBtn').click(function(){
                url = $(this).attr('id') + '.html';
                $('body').fadeOut(function(){
                    window.location = url;
                });     
            });
        });
    </script>
</head>
<body>
    <h1>index.html</h1>
    <div class="myBtn" id="index">index</div>
    <div class="myBtn" id="jobs">jobs</div>
</body>
</html>

http://jsfiddle/Dp4Hy/

PS. obviously the fiddle won't work, as you're trying to navigate to a new page, but you can still see the fade in at the beginning, and fade out when you click a button. Just need this script included for all pages to use.

Bottom line, this is not possible without some kind of pre-loading and interaction with a server side ponent

I would personally remend PJAX. http://pjax.heroku./ It allows you not only catch an event and load a document based on the event, it updates the browser state, url, title, the back button works, etc.

example sites that use it to acplish similiar behavior http://bleacherreport./articles/1716958-the-top-10-fantasy-qbs-for-2013 http://reciperehab./blog/post/the-6-best-salads-for-spring

*disclaimer, I did the second one...

Create your anchor tag and set a javascript onclick event. Call your fadeOut() function (which i've pasted below) You'll want it to fade out when you click, and when the next page loads, you'll want it to fade in:

jsfiddle: http://jsfiddle/HmGap/3/

HTML:

<script type="text/javascript">
window.onload=function(){fadeIn('body')};
</script>

<div id="body">
    Content <br /><br />
    <a onClick="fadeOut('body')" style="cursor:pointer">Click Me to Fade Out</a>
</div>

Javascript:

//fadeEffects

var fade_in_from = 0;
var fade_out_from = 10;

function fadeIn(element){
    var target = document.getElementById(element);
    target.style.display = "block";
    var newSetting = fade_in_from / 10;
    target.style.opacity = newSetting;
    // opacity ranges from 0 to 1
    fade_in_from++;
    if(fade_in_from == 10){
        target.style.opacity = 1;
        clearTimeout(loopTimer);
        fade_in_from = 0;
        return false;
    }
    var loopTimer = setTimeout('fadeIn(\''+element+'\')',100);
}
function fadeOut(element){
    var target = document.getElementById(element);
    var newSetting = fade_out_from / 10;
    target.style.opacity = newSetting;
    fade_out_from--;
    if(fade_out_from == 0){
        target.style.opacity = 0;
        target.style.display = "none";
        clearTimeout(loopTimer);
        fade_out_from = 10;
        return false;
    }
    var loopTimer = setTimeout('fadeOut(\''+element+'\')',100);
    window.location.href = "link.html";
}

Yes, it's possible, you can append the html in DIV (like you know), or you can use iframes, to manager the fade of the iframe tag

发布评论

评论列表(0)

  1. 暂无评论