Javascript jQuery BBQ:添加淡入淡出、滑动等过渡
第一次在这里问问题,但我热衷于使用stackoverflow 我已经使用jQuery BBQ创建了一个站点,以AJAX方式加载页面,并且仍然能够使用后退按钮跟踪历史记录,等等 开箱即用的BBQ只需使用Javascript jQuery BBQ:添加淡入淡出、滑动等过渡,javascript,jquery,jquery-bbq,Javascript,Jquery,Jquery Bbq,第一次在这里问问题,但我热衷于使用stackoverflow 我已经使用jQuery BBQ创建了一个站点,以AJAX方式加载页面,并且仍然能够使用后退按钮跟踪历史记录,等等 开箱即用的BBQ只需使用.hide()和.show()函数加载内容。我想知道,对于像淡入淡出这样的无缝过渡,最好的修改方法是什么 代码如下: $(function(){ // Keep a mapping of url-to-container for caching purposes. var cache =
.hide()
和.show()
函数加载内容。我想知道,对于像淡入淡出这样的无缝过渡,最好的修改方法是什么
代码如下:
$(function(){
// Keep a mapping of url-to-container for caching purposes.
var cache = {
// If url is '' (no fragment), display this div's content.
'': $('#home')
};
// Bind an event to window.onhashchange that, when the history state changes,
// gets the url from the hash and displays either our cached content or fetches
// new content to be displayed.
$(window).bind( 'hashchange', function(e) {
// Get the hash (fragment) as a string, with any leading # removed. Note that
// in jQuery 1.4, you should use e.fragment instead of $.param.fragment().
var url = $.param.fragment();
// Remove .current class from any previously "current" link(s).
$( 'a.current' ).removeClass( 'current' );
// Hide any visible ajax content.
$( '#main' ).children( ':visible' ).hide(1000);
// Add .current class to "current" nav link(s), only if url isn't empty.
url && $( 'a[href="#' + url + '"]' ).addClass( 'current' );
if ( cache[ url ] ) {
// Since the element is already in the cache, it doesn't need to be
// created, so instead of creating it again, let's just show it!
cache[ url ].show(1000);
} else {
// Show "loading" content while AJAX content loads.
$( '.bbq-loading' ).show();
// Create container for this url's content and store a reference to it in
// the cache.
//$('.page:visible').fadeout(1000);
url = $( '<div class="page"/>' )
// Append the content container to the parent container.
.appendTo( '#main' )
// Load external content via AJAX. Note that in order to keep this
// example streamlined, only the content in .infobox is shown. You'll
// want to change this based on your needs.
.load( url, function(){
// Content loaded, hide "loading" content.
$( '.bbq-loading' ).hide();
});
}
})
// Since the event is only triggered when the hash changes, we need to trigger
// the event now, to handle the hash the page may have loaded with.
$(window).trigger( 'hashchange' );
});
$(函数(){
//保留url到容器的映射以用于缓存。
变量缓存={
//如果url为“”(无片段),则显示此div的内容。
'':$('#家')
};
//将事件绑定到window.onhashchange,当历史记录状态更改时,
//从哈希中获取url,并显示缓存内容或回迁
//要显示的新内容。
$(窗口).bind('hashchange',函数(e){
//将散列(片段)作为字符串获取,删除任何前导字符
//在jQuery1.4中,应该使用e.fragment而不是$.param.fragment()。
var url=$.param.fragment();
//从任何以前的“当前”链接中删除.current类。
$('a.current').removeClass('current');
//隐藏任何可见的ajax内容。
$(“#main”).children(“:visible”).hide(1000);
//仅当url不为空时,才将.current类添加到“current”导航链接。
url&&$('a[href=“#”+url+''“]')。addClass('current');
如果(缓存[url]){
//由于元素已经在缓存中,因此不需要将其删除
//已创建,因此,与其再次创建,不如让我们展示它!
缓存[url]。显示(1000);
}否则{
//在加载AJAX内容时显示“加载”内容。
$('.bbq loading').show();
//为该url的内容创建容器,并在中存储对其的引用
//缓存。
//$('.page:visible')。淡出(1000);
url=$('')
//将内容容器附加到父容器。
.appendTo(“#main”)
//通过AJAX加载外部内容
//例如,仅显示.infobox中的内容
//希望根据您的需要对此进行更改。
.load(url,函数(){
//加载内容,隐藏“加载”内容。
$('.bbq loading').hide();
});
}
})
//由于事件仅在哈希更改时触发,因此我们需要触发
//事件,以处理页面可能已加载的哈希。
$(window.trigger('hashchange');
});
如您所见,我有一个.hide(1000)
和.show(1000)
,但它似乎会闪烁页面上的所有内容,甚至是
中不包含的元素
任何帮助都将不胜感激。很抱歉,我不能包含该示例,因为它是我和客户之间的机密信息。您应该选择组合jquery addClass+css,但也可以通过淡入淡出或动画在纯jquery中完成,如下所示:
//fadeOut the element
.fadeOut(1000);
.animate({opacity: 0}, 1000);
//fadeIn the element
.fadeIn(1000);
.animate({opacity: 1}, 1000);
隐藏
和显示
未设置动画。我想您需要fadeOut
和fadeIn
为hide
和show
添加一个持续时间来设置高度、宽度和不透明度的动画。我的目标是淡入淡出效果,您在fadeOut
和fadeIn
上的判断是正确的,尽管它似乎仍然会闪烁功能中没有针对性的元素。我已得到纠正。我建议设置一个演示(在像JSFIDLE这样的站点上)。