Issue with running jQuery function in another's callback
Trying to fade in a container once the image has been loaded, resized, and hidden a few elements, but am running into issues.
My console isn't showing any errors.. but the functions arn't executing
The commented out code worked perfectly.
Test site:
http://brantley.dhut.ch/
JavaScript:
/*jQuery(function($) {
$('.z').respond();
});*/
$(document).ready(function() {
//$('#two, #three, #four').hide();
//$('#main').fadeIn('slow');
$('.z').respond(function() {
$('#two, #three, #four').h开发者_JAVA技巧ide();
$('#main').fadeIn('slow');
});
$('.z').click(function() {
var slide = $(this);
slide.fadeOut(600, function() {
slide.next().fadeIn(600);
slide.insertAfter('.z:last');
});
});
});
Plugin:
(function( $ ){
$.fn.respond = function() {
/* initialize function on window load and resize */
$(document).ready(function() {
dimensions();
});
$(window).load(function() {
dimensions();
});
$(window).resize(function() {
dimensions();
});
/* declare affected elements */
var pic = this
/* set image's height or width depending on the browser window's size */
function dimensions() {
return pic.each(function() {
/* declare variables */
var browserWidth = $(window).width();
var imgRatio = $(this).width() / $(this).height()
var availableHeight = ($(window).height() - $('header').height() - $('footer').height() - 80)
var browserRatio = browserWidth / availableHeight
/* image sizing logic */
if (browserRatio >= imgRatio) {
if (browserWidth > 640) {
/* if the browser is landscape and bigger than mobile, set the image's height to fill the available space */
$(this).height(availableHeight).width('auto');
//$('body').css('background', 'blue');
} else {
/* it's mobile */
$(this).width(browserWidth - 40).height('auto');
//$('body').css('background', 'red');
}
} else {
/* if the browser is portrait, set the image's width to fill the page */
$(this).width(browserWidth - 40).height('auto');
//$('body').css('background', 'green');
}
/* horizontally center content */
$(this).css('margin-left', (browserWidth - $(this).width())/2);
});
};
};
})( jQuery );
your plugin has no callback defined, which is why nothing is happening.
change: $.fn.respond = function() {
to: $.fn.respond = function(callback) {
and add: callback();
at the end of the plugin.
精彩评论