我终于让 pjax 工作了,但我还有另一个问题。如何添加一些 jquery 动画,如淡出/滑动旧内容和淡入/滑动新内容?
默认情况下 pjax 只是更改内容而没有任何好看的效果..
任何帮助将非常感激。
此致
我终于让 pjax 工作了,但我还有另一个问题。如何添加一些 jquery 动画,如淡出/滑动旧内容和淡入/滑动新内容?
默认情况下 pjax 只是更改内容而没有任何好看的效果..
任何帮助将非常感激。
此致
基本上,你有一堆事件可以锁定并随心所欲地做。这是使用fadeIn
and作为触发器的基本fadeOut
版本 。pjax:start
pjax:end
$(document)
.on('pjax:start', function() { $('#main').fadeOut(200); })
.on('pjax:end', function() { $('#main').fadeIn(200); })
显然,您将交换#main
要进出内容的容器元素。
Let's say you want to create a directory browser, just like GitHub.
Let's start with Twitter Bootstrap's carousel and create a view with markup to use it (hope you don't mind haml):
Here's the container:
%div#uber-glider.glider.carousel.span12
%div.carousel-inner
= yield
%div#uber-glider-stage.glider-stage(style="display:none")
And here's a sample partial to render the pjaxed content within it:
%div.glider-group.item
%div.glider-heading(data-title="#{@super_department.name} Super Department" data-resource="#{super_department_path @super_department.id}")
%ul.breadcrumb
%li
%a.glider-link(href="#{divisions_path}")= "Divisions"
%span.divider= "/"
%li.active
%a.glider-link(href="#{division_path @division.id}")= @division.name
%span.divider= "/"
%li.active
= @super_department.name
%div.glider-body
- @super_department.departments.each_with_index do |department, i|
%div.glider-listing
%a.glider-link(data-glide="descend" data-target="#uber-glider" href="#{department_path department.id}")
= department.name
Now let's create some Javascript with pjax:
(function($){
"use strict";
$(function() {
$('#uber-glider-stage').on('pjax:success', function(e){
var $incoming_group = $('#uber-glider-stage .glider-group')
, $incoming_heading = $('#uber-glider-stage .glider-heading')
, incoming_resource = $incoming_heading.data('resource')
, $existing_groups = $('#uber-glider .glider-group')
, $existing_headings = $('#uber-glider .glider-heading')
, existing_last_idx = $existing_groups.length - 1
, matching_idx = -1;
$existing_headings.each(function(idx) {
if ($(this).data('resource') === incoming_resource) {
matching_idx = idx;
}
});
if (matching_idx > -1) {
// pop
$incoming_group.remove();
$('#uber-glider').one('slid', function() {
for (; existing_last_idx > matching_idx; existing_last_idx--) {
$('#uber-glider .glider-group').last().remove();
}
});
$('#uber-glider').carousel(matching_idx);
}
else {
// push
debug.debug("pushing 1 level");
$incoming_group.detach();
var $container = $('#uber-glider > .carousel-inner');
$container.append($incoming_group);
$('#uber-glider').carousel('next');
}
});
$('.glider-link').pjax('#uber-glider-stage', { 'timeout' : '3000' }).on('click', function(){
debug.debug(".glider-link click");
});
});
$('#uber-glider .carousel-inner .item').addClass('active');
$('#uber-glider').carousel('pause');
})(window.jQuery);
我想加载指示器和动画或多或少是一回事。在这种情况下,使用 pjax:send 和 pjax:complete 事件。
如果您正在实现加载指示器,则发送和完成是一对很好的事件。只有在发出实际请求时才会触发它们,而不是从缓存中加载它们。