简体   繁体   中英

Transition between html pages using jquery to load a div?

I am trying to use jQuery to create a stylish transition between HTML pages.

however, I just need to fade in and fade out the content of the html pages.

for example:

I have two pages 1.html and 2.html

they both have a DIV called #content

I need to only fadein and fade out the div content instead of the entire html page.

I am using the code bellow:

<script type="text/javascript">
var speed = 'slow';

$('html, body').hide();
$(document).ready(function() {
    $('html, body').fadeIn(speed, function() {
        $('a[href], button[href]').click(function(event) {
            var url = $(this).attr('href');
            if (url.indexOf('#') == 0 || url.indexOf('javascript:') == 0) return;
            event.preventDefault();
            $('html, body').slideToggle(speed, function() {
                window.location = url;
            });
        });
    });
});
</script>

I've tried it like this as well and it didn't do anything:

<script type="text/javascript">
var speed = 'slow';

$('html, body').hide();
$(document).ready(function() {
    $('html, body #content').fadeIn(speed, function() {
        $('a[href], button[href]').click(function(event) {
            var url = $(this).attr('href');
            if (url.indexOf('#') == 0 || url.indexOf('javascript:') == 0) return;
            event.preventDefault();
            $('html, body').slideToggle(speed, function() {
                window.location = url;
            });
        });
    });
});
</script>

is this possible?

Thanks

Try this code

$(document).ready(function() {

    var hash = window.location.hash.substr(1);
    var href = $('a[href], button[href]').each(function(){
        var href = $(this).attr('href');
        if(hash==href.substr(0,href.length-5)){
            var toLoad = hash+'.html #content';
            $('#content').load(toLoad)
        }                                           
    });

Click Function

$('a[href], button[href]').click(function(){

        var toLoad = $(this).attr('href')+' #content';
        $('#content').hide('fast',loadContent);
        $('#load').remove();
        $('#load').fadeIn('normal');
        window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
        function loadContent() {
            $('#content').load(toLoad,'',showNewContent())
        }
        function showNewContent() {
            $('#content').show('normal',hideLoader());
        }
        function hideLoader() {
            $('#load').fadeOut('normal');
        }
        return false;

    });

});

Refer this fiddle

http://jsfiddle.net/fBrYp/2/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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