changing $ sign with jQuery - javascript

Well i'm trying to make Masonry work with infinite-scroll, and i found the official appended method (http://desandro.github.io/masonry/demos/infinite-scroll.html) and tried to make it work on my code. but i needed to change some of the $ to jQuery and now it's working as masonry but the infinte scroll still not working, and i'm wondering if i forgot a dollar sign to change to jQuery from my code, Please help me
<script >
jQuery(function(){
var $container = jQuery('ul.products');
$container.imagesLoaded(function(){
$container.masonry({
itemSelector: 'li.product',
columnWidth : 295,
isFitWidth: true,
gutterWidth : 2
});
});
$container.infinitescroll({
navSelector : '#page-nav-woo', // selector for the paged navigation
nextSelector : '.next', // selector for the NEXT link (to page 2)
itemSelector : 'li.product', // 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 = jQuery( 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>

Ok the solution for my problem is like this:
1.i installed the wp-plugin
2.for callback section added:
var $newElems = jQuery( 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.append( $newElems ).masonry( 'appended', $newElems, true );
});
3.behavior: Masonry
And it works like a charm! Thanks all!

Alternatively you can wrap your script in a closure:
(function($) {
// i can use $ instead of jQuery
} (jQuery));
or
(function(jQuery) {
// i can use jQuery instead of $
} ($));

Related

Infinite Scroll + Masonry

I am trying to get infinite scroll to work with Masonry.
+function ($) {
var $container = $('.masonry');
$container.imagesLoaded(function(){
$container.masonry({
columnWidth: '.grid-sizer',
gutter: '.gutter-sizer',
itemSelector: '.item'
})
});
$container.infinitescroll({
navSelector : '#page-nav', // selector for the paged navigation
nextSelector : '#page-nav a', // selector for the NEXT link (to page 2)
itemSelector : '.item', // 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 );
});
}
);
}(jQuery);
It seems if I remove the imagesLoaded function and just call the masonry, it shows the images how Masonry intended, but doesn't infinitely scroll.
As it is I get an error:
Uncaught TypeError: undefined is not a function
I am using Foundation and I am calling my scripts in this order:
#import 'vendor/masonry.pkgd.js';
#import 'vendor/jquery.infinitescroll.min.js';
#import 'scripts.js';
Scripts included the code I have highlighted at the start. jQuery version is 2.0.3
you will have to add the imagesloaded library.
try this code
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-infinitescroll/2.0b2.120519/jquery.infinitescroll.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/masonry/3.1.2/masonry.pkgd.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery.imagesloaded/3.0.4/jquery.imagesloaded.min.js"></script>
(function() {
// Main content container
var $container = $('.masonry');
// Masonry + ImagesLoaded
$container.imagesLoaded(function(){
$container.masonry({
// selector for entry content
columnWidth: '.grid-sizer',
gutter: '.gutter-sizer',
itemSelector: '.item'
});
});
// Infinite Scroll
$container.infinitescroll({
// selector for the paged navigation (it will be hidden)
navSelector : "#page-nav",
// selector for the NEXT link (to page 2)
nextSelector : "#page-nav a",
// selector for all items you'll retrieve
itemSelector : ".item",
// finished message
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 );
});
});
/**
* OPTIONAL!
* Load new pages by clicking a link
*/
// Pause Infinite Scroll
$(window).unbind('.infscr');
// Resume Infinite Scroll
$('.#page-nav a').click(function(){
$container.infinitescroll('retrieve');
return false;
});
})();

Jquery function not recognized if jquery file is not included in the included jsp

Hi have a Main Jsp file where I have included jquery.infinite-scroll.js. This main.jsp has one included jsp say incl.jsp. Now the function for infinite scrolling is defined in the main jsp like
$(document).ready(function () {
$(function(){
var $container = $('#container');
$container.imagesLoaded( function(){
$container.isotope({
itemSelector: '.box',
gutterWidth: 10
});
});
$container.infinitescroll({
navSelector : '#page-nav', // selector for the paged navigation
nextSelector : '#page-nav a', // selector for the NEXT link (to page 2)
itemSelector : '.box', // selector for all items you'll retrieve
debug: true,
loading_image: 'http://i.imgur.com/6RMhx.gif',
loading: {
finishedMsg: 'No more pages to load.',
img: 'http://i.imgur.com/6RMhx.gif'
}
},
// trigger Masonry as a callback
function( newElements ) {
console.log('I m here');
// 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.isotope( 'appended', $newElems, true );
});
}
);
});
});
Here container div is in the main.jsp
<div id="container" class="transitions-enabled infinite-scroll clearfix centered">
<%# include file="/incl.jsp" %>
</div>
Now if I do not include the infinite-scroll.js in this included jsp, browser is throwing error that $container.infinitescroll is not a function.
Why do I have to include the js file in the included jsp?

jQuery error TypeError: $.event.handle is undefined

I am using latest jQuery for jQuery.masonry its throwing following error: on line 47 in jquery-1.9.1.min.js
TypeError: $.event.handle is undefined
if any one is having same error ?
MyCode:
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/scripts/jquery-1.9.1.min.js"></script>
<script language="javascript" src="http://masonry.desandro.com/jquery.masonry.min.js"></script>
<script src="http://masonry.desandro.com/js/jquery.infinitescroll.min.js"></script>
<script>
$(function(){
var $container = $('#content');
$container.imagesLoaded(function(){
$container.masonry({
itemSelector: '.post',
isAnimated: true
});
});
$container.infinitescroll({
navSelector : '.wp-pagenavi', // selector for the paged navigation
nextSelector : '.wp-pagenavi a', // selector for the NEXT link (to page 2)
itemSelector : '.post', // 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>
The function has been deprecated: http://jquery.com/upgrade-guide/1.9/#other-undocumented-properties-and-methods
You can use $.event.dispatch instead.
In addition or alternatively to using the dispatch function you can add the Migrate plugin, http://blog.jquery.com/2013/05/01/jquery-migrate-1-2-0-released/, which will add back in $.event.handle so you able to fix the code without breaking the application.
You are trying to use :
var $newElems = $( newElements ).css({ opacity: 0 });
as an element :
$newElems.imagesLoaded
Maybe this is the problem.
A solution would be :
var $newElems = $( newElements );
$newElems.css({ opacity: 0 });

Masonry - Rails integration issue

How to catch "GET" parameter of masonry infinite scrolling for show number of page in url address.
JS:
$(function(){
var $container = $('#masonry-container');
$container.imagesLoaded(function(){
$container.masonry({
itemSelector: '.box',
columnWidth: 100
});
});
$container.infinitescroll({
navSelector : '#page-nav', // selector for the paged navigation
nextSelector : '#page-nav a', // selector for the NEXT link (to page 2)
itemSelector : '.box', // 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 );
});
}
);
});
How to find
vk_posts?page=2
and save to var.
After scrolling I have localhost:3000/vk_posts in all loaded pages. Masonry deletes selector $(".pagination a") and I cant to find href of this selector.

Trigger infinitescroll manually

I am using the following code to trigger infinitescroll on isotope masonry, however how do I use the "Click to load more posts" instead, "the manual trigger". I tried implementing from solutions on the internet but they do not work for me. Thanks.
/*--------------------------------------------------------------------------------*/
/* infinitescroll
/*--------------------------------------------------------------------------------*/
jQuery(document).ready(function($) {
var $container = $('.masonry');
$container.imagesLoaded( function(){
$container.isotope({
itemSelector : '.item'
});
});
$container.infinitescroll({
// selector for the paged navigation
navSelector : '.post-nav',
// selector for the NEXT link (to page 2)
nextSelector : '.post-nav .prev-post a',
// selector for all items you'll retrieve
itemSelector : '.item',
loading: {
finishedMsg: 'No more pages to load.',
img: 'http://i.imgur.com/qkKy8.gif'
}
},
function( newElements ) {
var $newElems = $( newElements ).css({ opacity: 0 });
$newElems.imagesLoaded(function() {
$newElems.animate({ opacity: 1 });
$container.isotope( 'appended', $( newElements ) );
$container.isotope('reLayout');
});
});
});
Assuming that you're using this plugin, the following should work to manually trigger a retrieve:
$container.infinitescroll('retrieve');

Categories

Resources