Isotope + Wordpress - re-layout after posts load? - php

I am using Isotope with a masonry layout in WordPress, along with ImagesLoaded. Each post is a separate isotope item. All seems to be working well, except when I load something dynamic in the post, like a twitter widget or a embedded video. The initial height of the item is set before the post/widget has loaded. Once it has fully loaded, I need isotope to re-layout again to accommodate the new height.
Is there a way to call the layout function again after all the posts are fully loaded?
Here is my isotope code:
// init isotope
var $container = $('#cards');
$container.isotope({
itemSelector: '.item',
masonry: {
columnWidth: '.grid-sizer',
isFitWidth: true
}
});
// layout Isotope again after all images have loaded
$container.imagesLoaded( function() {
$container.isotope('layout');
});
// infinite scroll
$container.infinitescroll({
navSelector : '.pagination',
nextSelector : '.pagination a',
itemSelector : '.item-scroll',
behavior: "twitter",
loading: {
finishedMsg: 'No more pages to load.'
}
},
function ( newElements ) {
var $newElems = jQuery( newElements ).hide(); // hide to begin with
// ensure that images load before adding to masonry layout
$newElems.imagesLoaded(function(){
$newElems.fadeIn(); // fade in when ready
$container.isotope( 'appended', $newElems );
});
}
);
function onLayout() {
$container.infinitescroll('scroll');
}
Thank you for your help!

Try this:
var $container = $('#cards');
$container.imagesLoaded( function() {
// init isotope
$container.isotope({
itemSelector: '.item',
masonry: {
columnWidth: '.grid-sizer',
isFitWidth: true
}
});
});
// infinite scroll
$container.infinitescroll({
navSelector : '.pagination',
nextSelector : '.pagination a',
itemSelector : '.item-scroll',
behavior: "twitter",
loading: {
finishedMsg: 'No more pages to load.'
}
},
function ( newElements ) {
var $newElems = jQuery( newElements ).hide(); // hide to begin with
// ensure that images load before adding to masonry layout
$newElems.imagesLoaded(function(){
$newElems.fadeIn(); // fade in when ready
$container.isotope( 'appended', $newElems ).isotope('layout');
});
}
);
function onLayout() {
$container.infinitescroll('scroll');
}

Related

WordPress Ajax call very slow and jittery

I've been working on a website for some time now and while they do have a large amount of content and I have upgraded them, the AJAX load more call on the masonry grid is very slow. I have tried caching and using a CDN but it's still taking a very long time, particularly after the first instance.
Does anyone have any ideas? Website is www.noctismag.com
Here's the script I'm using to run it, in my footer.
<script>
jQuery(function ($) {
/* Masonry + Infinite Scroll */
var $container = $('#grid-container');
$container.imagesLoaded(function () {
$container.masonry({
itemSelector: '.post'
});
});
$('#grid-container').masonry({
itemSelector: '.post'
, columnWidth: 258
});
$container.infinitescroll({
navSelector: '#page-nav'
, nextSelector: '#page-nav a'
, itemSelector: '.post'
}, function (newElements) {
var $newElems = $(newElements).css({
opacity: 0
});
$newElems.imagesLoaded(function () {
$newElems.animate({
opacity: 1
});
$container.masonry('appended', $newElems, true);
});
});
$(window).unbind('.infscr');
jQuery("#page-nav a").click(function () {
jQuery('#grid-container').infinitescroll('retrieve');
return false;
});
$(document).ajaxError(function (e, xhr, opt) {
if (xhr.status == 404) $('#page-nav a').remove();
});
});
</script>
Does the Ajax request send back HTML code?
If it's the case try to change the code, Ajax call must return data as a JSON string and a front-end function will transform that data to render it on grid.

How to enable drag and drop on double click of a image?

I am working on drag and drop of images.my drag and drop is working fine.when i drag any image from sidebar and drop it then it shows a control box with dragged item and all this is working fine. Now i need to do the same functionality on click or double click of a image.when user will click any image in sidebar then that image will be automatically dropped in the dropable div area. This is my drag and drop code. Do i need to do same code again on double click of an image?
jQuery(document).ready(function ()
{
dragablediv();
});
function dragablediv()
{
var x = null;
jQuery(".get-image .drag").draggable({
helper: 'clone',
cursor: 'move',
tolerance: 'fit',
revert: true,
/* stop: function (event, ui) { ... } */
});
jQuery(".droppable").droppable({
accept: '.drag',
activeClass: "drop-area",
drop: function (e, ui) {
if (jQuery(ui.draggable)[0].id != "") {
x = ui.helper.clone();
ui.helper.remove();
jQuery("#droppable .resize-img").css({"display":"inline-table"});
/* To show Controls first time on Dragged Image*/
var droppedItemId = ui.draggable.attr("data-item-id");
jQuery(".controls ").attr("controls-item-id",droppedItemId);
jQuery("#droppable").find(".ui-resizable-handle").hide(); // To show Controls
jQuery(".controls").show();
dragEl = jQuery(this);
stopPosition = dragEl.position();
var height = x.height(); // To show the controls with exact position
var width = x.width(); //alert("stoppos"+stopPosition+"leftpos"+leftPos);
var topPos = ui.position.top;
var leftPos = ui.position.left;
var mytop = topPos+height;
var myleft = leftPos + width /2;
//alert("left"+leftPos);
jQuery(".controls").css({"top": mytop , "left":myleft, "display":"block"});
x.draggable({
helper: 'original',
cursor: 'move',
containment: '#droppable',
tolerance: 'fit',
drop: function (event, ui) {
jQuery(ui.draggable).remove();
},
drag: function() {
},
stop: function() {
dragEl = jQuery(this);
stopPosition = dragEl.position();
var leftPos = dragEl.position().left;
var topPos = dragEl.position().top;
stopOffset = dragEl.offset();
document_id = dragEl.data("document-id");
signer_id = dragEl.data("signer-id");
var height = x.height();
var width = x.width();
var mytop = stopPosition.top+height;
//var myleft = stopPosition.left + width /2;
jQuery(".controls").css({"top": mytop , "left":myleft, "display":"block"});
dataItem = jQuery(this).attr('data-item-id');
jQuery("#droppable").find(".ui-resizable-handle").hide();
jQuery(".controls").hide();
jQuery(this).closest(".drag").find(".ui-resizable-handle").show();
jQuery(".controls").show();
jQuery(".controls ").attr("controls-item-id",dataItem);
}
});
x.resizable({
maxHeight: jQuery('#droppable').height(),
maxWidth: jQuery('#droppable').width(),
aspectRatio: true,
helper: "ui-resizable-helper",
handles: "nw, ne, sw,se"
});
//x.addClass('remove');
x.appendTo('.droppable');
}
}
});
}

Opening php file in mymodal box and passing a variable to it

I have a modal box that opens content from the footer of a page (a hidden div). I am trying to launch the modal and display the content of another .php file whilst passing a variable that can be used to SELECT from a DB Any ideas?
Here is the code:
The modal box link
Click Me For A Modal
The JS
(function($) {
$('a[data-reveal-id]').live('click', function(e) {
e.preventDefault();
var modalLocation = $(this).attr('data-reveal-id');
$('#'+modalLocation).reveal($(this).data());
});
The .php file with the modalbox content
<div id="myModal" class="reveal-modal">
<div>content called from DB using passed variable</div>
<p>more content</p>
<a class="close-reveal-modal">×</a>
Does anyone have any ideas please?
-----------------------------------------------------------------------------------
UPDATED!
Here is the full js file:
(function($) {
$('a[data-reveal-id').live('click', function(e)
{
e.preventDefault();
var modalLocation = $(this).attr('data-reveal-id');
$.ajax({
url: 'code.php',
data: '$varible',
type: 'GET',
error: function()
{
// If there's an issue, display an error...
},
success: function(output)
{
$('#' + modalLocation).innerHTML(output).reveal( $(this).data() );
}
});
})
$.fn.reveal = function(options) {
var defaults = {
animation: 'fadeAndPop', //fade, fadeAndPop, none
animationspeed: 300, //how fast animtions are
closeonbackgroundclick: true, //if you click background will modal close?
dismissmodalclass: 'close-reveal-modal' //the class of a button or element that will close an open modal
};
var options = $.extend({}, defaults, options);
return this.each(function() {
var modal = $(this),
topMeasure = parseInt(modal.css('top')),
topOffset = modal.height() + topMeasure,
locked = false,
modalBG = $('.reveal-modal-bg');
if(modalBG.length == 0) {
modalBG = $('<div class="reveal-modal-bg" />').insertAfter(modal);
}
//Entrance Animations
modal.bind('reveal:open', function () {
modalBG.unbind('click.modalEvent');
$('.' + options.dismissmodalclass).unbind('click.modalEvent');
if(!locked) {
lockModal();
if(options.animation == "fadeAndPop") {
modal.css({'top': $(document).scrollTop()-topOffset, 'opacity' : 0, 'visibility' : 'visible'});
modalBG.fadeIn(options.animationspeed/2);
modal.delay(options.animationspeed/2).animate({
"top": $(document).scrollTop()+topMeasure + 'px',
"opacity" : 1
}, options.animationspeed,unlockModal());
}
if(options.animation == "fade") {
modal.css({'opacity' : 0, 'visibility' : 'visible', 'top': $(document).scrollTop()+topMeasure});
modalBG.fadeIn(options.animationspeed/2);
modal.delay(options.animationspeed/2).animate({
"opacity" : 1
}, options.animationspeed,unlockModal());
}
if(options.animation == "none") {
modal.css({'visibility' : 'visible', 'top':$(document).scrollTop()+topMeasure});
modalBG.css({"display":"block"});
unlockModal()
}
}
modal.unbind('reveal:open');
});
//Closing Animation
modal.bind('reveal:close', function () {
if(!locked) {
lockModal();
if(options.animation == "fadeAndPop") {
modalBG.delay(options.animationspeed).fadeOut(options.animationspeed);
modal.animate({
"top": $(document).scrollTop()-topOffset + 'px',
"opacity" : 0
}, options.animationspeed/2, function() {
modal.css({'top':topMeasure, 'opacity' : 1, 'visibility' : 'hidden'});
unlockModal();
});
}
if(options.animation == "fade") {
modalBG.delay(options.animationspeed).fadeOut(options.animationspeed);
modal.animate({
"opacity" : 0
}, options.animationspeed, function() {
modal.css({'opacity' : 1, 'visibility' : 'hidden', 'top' : topMeasure});
unlockModal();
});
}
if(options.animation == "none") {
modal.css({'visibility' : 'hidden', 'top' : topMeasure});
modalBG.css({'display' : 'none'});
}
}
modal.unbind('reveal:close');
});
//Open Modal Immediately
modal.trigger('reveal:open')
//Close Modal Listeners
var closeButton = $('.' + options.dismissmodalclass).bind('click.modalEvent', function () {
modal.trigger('reveal:close')
});
if(options.closeonbackgroundclick) {
modalBG.css({"cursor":"pointer"})
modalBG.bind('click.modalEvent', function () {
modal.trigger('reveal:close')
});
}
$('body').keyup(function(e) {
if(e.which===27){ modal.trigger('reveal:close'); } // 27 is the keycode for the Escape key
});
function unlockModal() {
locked = false;
}
function lockModal() {
locked = true;
}
});//each call
}//orbit plugin call
})(jQuery);
Here is the html trigger:
<a class="big-link" href="#" data-reveal-id="myModal">Click Me to open modal</a>
Here is the code.php, file containing the modal:
<div id="myModal" class="reveal-modal"><div>content called from DB using passed variable</div><p>more content</p><a class="close-reveal-modal">×</a>
The issue at the minute is not passing the variable, but actually lauching the modal with the content of code.php
Thanks again for your time with this problem!
Without knowing what variables you're looking to pass, and what you're trying to grab, you can modify this. I haven't tested it so you may have to do some tweaking.
$('a[data-reveal-id').live('click', function(e)
{
e.preventDefault();
var modalLocation = $(this).attr('data-reveal-id');
$.ajax({
url: 'URL_TO_YOUR_PHP',
data: 'YOUR_VARIABLE', // Look at how to pass data using GET, or POST
type: 'GET' or 'POST', // Choose one, and pass the data above appropriately
error: function()
{
// If there's an issue, display an error...
},
success: function(output)
{
$('#' + modalLocation).innerHTML(output).reveal( $(this).data() );
}
});
})
-- EDIT --
Now the problem is that you don't have #myModal available in your HTML, yet. So, what you want to do is change the following line accordingly:
$('#' + modalLocation).innerHTML(output).reveal( $(this).data() );
-- BECOMES --
$("body").append(output).reveal( $(this).data() );
In your CSS you'll want to initially hide your modal box that way it's revealed appropriately.

Too much recursion on ajax call

I am usining masonry view to display content with infinite scrolling functionality.
Masonry view part is working fine. For infinite scroll I have tried infinitescroll js
or on the basis of scroll as I have written below code.
Problem :- After first scroll I am facing too much recursion problem.
jQuery(document).ready(function($) {
var $container = jQuery('.main_container');
$container.imagesLoaded(function(){
// options
$container.masonry({
itemSelector: '.pin',
isAnimated: true,
isFitWidth: true,
isAnimatedFromBottom: true
});
});
//for infinite scrollings
jQuery(window).scroll(function() {
if(jQuery(window).scrollTop() + jQuery(window).height() == jQuery(document).height()) {
alert("bottom!");
ajaxurl = "script url here";
var data = {start:startLimit,end:endLimit};
jQuery.get(ajaxurl, data, function(response) {
var $boxes = $(response);
$('.main_container').append( $boxes ).masonry( 'appended', $boxes );
});
}
});
});
I am trying this on wordpress admin section plugin.
After step-by-step checking I found solution , Cause of the problem I am using animate effect in masonry which is conflict some how with wordpress plugin view js.
$container.imagesLoaded(function(){
// options
$container.masonry({
itemSelector: '.pin',
isAnimated: false,
isFitWidth: true,
isAnimatedFromBottom: false
});
});

Jquery Infinite-Scroll not infinite past page 2

I'm using infinite scroll with dynamic data but can't seem to get past page 2...
When the page initially loads I have an offset in the URL so we start at 0, so,
test2.html?offset=0
this is the code to load the date
$offset = $_GET['offset'];
$data = mysql_query("select * from list limit 30 offset $offset;",$db);
echo '<div id="wall" class="transitions-enabled infinite-scroll clearfix">';
while ($databack33 = mysql_fetch_array($data)){
echo '<div class="block">';
echo '';
echo '</div>';
}
Then to load the next page i use:
<nav id="page-nav">
<? $offset = $offset+30; ?>
</nav>
This works ok for pages one and two but it then tells me no more pages to load although there is more data.
If I look at the page source it is correct test2.html?offset=60
this is the masonry/infinite scroll set up
<script type="text/javascript">
$(function(){
var $container = $('#wall');
$container.imagesLoaded(function(){
$container.masonry({
itemSelector: '.block',
isAnimated: true,
animationOptions: {
duration: 750,
easing: 'linear',
queue: false
}
});
});
$container.infinitescroll({
navSelector : '#page-nav', // selector for the paged navigation
nextSelector : '#page-nav a', // selector for the NEXT link (to page 2)
itemSelector : '.block', // selector for all items you'll retrieve
loading: {
finishedMsg: 'No more pages to load.',
img: 'http://i.imgur.com/6RMhx.gif'
}
},
// trigger Masonry as a callback
function( newElements ) {
// hide new items while they are loading
var $newElems = $( newElements ).css({ opacity: 0 });
// ensure that images load before adding to masonry layout
$newElems.imagesLoaded(function(){
// show elems now they're ready
$newElems.animate({ opacity: 1 });
$container.masonry( 'appended', $newElems, true );
});
}
);
});
</script>
To get infinitescroll working with "offset=" pagination I did the following:
Store the current 'next page' url, and get the offset value.
var nextSelector = '#page-nav a';
var origNextUrl = $(nextSelector).attr('href');
var offsetRegex = /(offset=)([0-9]+)/;
var offset = origNextUrl.match(offsetRegex)[2];
Then in infinitescroll assign a function to the 'path' option which accepts a page number and returns the url used to load the next page.
$container.infinitescroll({
// other options
path: function(pageNum) {
return origNextUrl.replace( offsetRegex, ("$1" + (offset * pageNum)) );
},
//callback
});

Categories