在另一个回调中运行 jQuery 函数时出现问题
图像加载、调整大小并隐藏一些元素后,尝试淡入容器,但遇到了问题。
我的控制台没有显示任何错误..但是函数没有执行
注释掉的代码工作得很好。
测试站点:
JavaScript:
/*jQuery(function($) {
$('.z').respond();
});*/
$(document).ready(function() {
//$('#two, #three, #four').hide();
//$('#main').fadeIn('slow');
$('.z').respond(function() {
$('#two, #three, #four').hide();
$('#main').fadeIn('slow');
});
$('.z').click(function() {
var slide = $(this);
slide.fadeOut(600, function() {
slide.next().fadeIn(600);
slide.insertAfter('.z:last');
});
});
});
插件:
(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 );
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:
JavaScript:
/*jQuery(function($) {
$('.z').respond();
});*/
$(document).ready(function() {
//$('#two, #three, #four').hide();
//$('#main').fadeIn('slow');
$('.z').respond(function() {
$('#two, #three, #four').hide();
$('#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 );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的插件没有定义回调,这就是为什么没有发生任何事情。
将:
$.fn.respond = function() {
更改为:
$.fn.respond = function(callback) {
并添加:
callback();< /code> 在插件末尾。
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.