jQuery脚本的顺序正确吗?隐藏/显示功能在实时版本中不起作用,但可以作为独立Fiddle使用
Proper order of jQuery script? Hide/Show function not working in live version but works as stand alone Fiddle
我有几个jQuery函数工作正常-一个固定徽标和导航菜单的fadeIn绑定到页面滚动,以及一个用于Firefox的灯箱和滚动条修复绑定到(document).ready。
我正在尝试添加隐藏和/或显示与单击相关的几个div的功能。
作为一个独立的功能,我让它像小提琴一样正常工作。。。
在这里工作JSFiddle
问题是,我正在学习jQuery,我不确定调用页面底部脚本中函数的正确顺序。
我的逻辑告诉我将其包含在(document).ready部分下,但当我从小提琴插入工作脚本时,它不仅不起作用,而且会破坏我的其余功能。
当前没有隐藏/显示功能的工作脚本(根据注释更新):
<script>
$(window).scroll(function() {
if ($(this).scrollTop() > 200) {
$(".main-links").fadeIn();
$(".header-logo").fadeIn();
} else {
$(".main-links").hide();
$(".header-logo").hide();
}
});
$(document).ready(function(){
$('.lightbox').nivoLightbox();
$('.scroll-pane').jScrollPane();
$('.scroll-pane2').jScrollPane();
});
</script>
我尝试插入附加功能的方式如下:
<script>
$(window).scroll(function() {
if ($(this).scrollTop() > 200) {
$(".main-links").fadeIn();
$(".header-logo").fadeIn();
} else {
$(".main-links").hide();
$(".header-logo").hide();
}
});
$(document).ready(function(){
$('.lightbox').nivoLightbox();
$('.scroll-pane').jScrollPane();
$('.scroll-pane2').jScrollPane();
$('#menu-switch1').on('click', function(event) {
$('#main-tasting-menu').toggle('show');
$('#dessert-tasting-menu').toggle('hide');
});
$('#menu-switch2').on('click', function(event) {
$('#main-tasting-menu').toggle('hide');
$('#dessert-tasting-menu').toggle('show');
});
</script>
当我这样做的时候,切换不会像在我的小提琴里那样工作,它会杀死我的滚动条2,并添加一个不应该在.woll-pane1上的水平滚动条。
有没有因为我对jQuery的糟糕了解而明显忽略了什么?
HTML部分(脚本清单?):
<script src="js/nivo-lightbox.min.js"></script>
<link href="css/nivo-lightbox.css" rel="stylesheet" type="text/css" media="screen">
<link href="themes/default/default.css" rel="stylesheet" type="text/css" media="screen">
<!-- jScrollPane CSS for Firefox scrollbar fix (applied to all browsers) -->
<link href="css/jquery.jscrollpane.css" rel="stylesheet" type="text/css" media="screen">
<!-- Mousewheel support for jScrollPane scrollbar fix for FireFox and IE -->
<script type="text/javascript" src="js/jquery.mousewheel.js"></script>
<!-- the jScrollPane script -->
<script type="text/javascript" src="js/jquery.jscrollpane.min.js"></script>
首先,在为#menu-switch
添加单击处理程序时,您的示例中出现语法错误。你需要关闭函数和调用才能激活,如下所示。请注意,额外的})
关闭了对live()
的每次调用。
$(document).ready(function(){
$('.lightbox').nivoLightbox();
$('.scroll-pane').jScrollPane();
$('.scroll-pane2').jScrollPane();
$('#menu-switch1').live('click', function(event) {
$('#main-tasting-menu').toggle('show');
$('#dessert-tasting-menu').toggle('hide');
});
$('#menu-switch2').live('click', function(event) {
$('#main-tasting-menu').toggle('hide');
$('#dessert-tasting-menu').toggle('show');
});
});
还有一些事情你可能需要考虑。您将希望在$(document).ready()
调用中封装所有内容,因为这将确保jQuery和DOM已准备好执行操作。正如charlietfl在上面的一条评论中提到的,live()
方法在1.7版本中被弃用。如果您使用的版本>1.7,那么您将需要使用on()
。这是整件事的更新版本。
$(document).ready(function(){
// Cache the items you need to use inside different functions.
// This is so that you don't have to call the jQuery method
// on every user action.
var $mainLinks = $('.main-links');
var $headerLogo = $('.header-logo');
var $mainTastingMenu = $('#main-tasting-menu');
var $dessertTastingMenu = $('#dessertTastingMenu');
var $menuSwitch_1 = $('#menu-switch1');
var $menuSwitch_2 = $('#menu-switch2');
// Setup your plugins
$('.lightbox').nivoLightbox();
$('.scroll-pane').jScrollPane();
$('.scroll-pane2').jScrollPane();
// Setup your event handlers
$menuSwitch_1.on('click', function(event) {
$mainTastingMenu.toggle('show');
$dessertTastingMenu.toggle('hide');
});
$menuSwitch_2.on('click', function(event) {
$mainTastingMenu.toggle('hide');
$dessertTastingMenu.toggle('show');
});
// I made this handler match the ones above for consistency
$(window).on('scroll', function(event) {
if ($(this).scrollTop() > 200) {
$mainLinks.fadeIn();
$headerLogo.fadeIn();
} else {
$mainLinks.hide();
$headerLogo.hide();
}
});
});
相关文章:
- 一页签出在Magento版本1.7.0.2中不起作用
- pixijs 鼠标关闭事件不起作用(任何版本)
- 为什么我的谷歌浏览器扩展程序在谷歌浏览器版本 41.0.2272.89 中不起作用
- Phonegap (Android) - 数据库更改版本不起作用
- jquery.min.js版本冲突在asp.net网站中不起作用
- Kineticjs:drag.moving 事件在最新版本上不起作用.如何升级代码
- Jssor 全长滑块在 opera 9.64 版本中不起作用
- Chrome 消息:chrome.runtime.sendMessage 在最新版本 49 上不起作用
- jQuery 引导选项卡在自定义模式中不起作用(不是引导模式,而是我自己的版本)
- Javascript 函数在 IE(仅限)版本 9 - 10 上不起作用
- 画布元素在 IE 8 及更低版本中不起作用
- html下拉菜单在Firefox中不起作用,并且在旧版本的IE中一切都是错误的
- 砌体在移动浏览器(chrome)上不起作用,并且仅在Chrome桌面版本上有效,如果我重新加载(ctrl + f5)页面
- Meteor 1.2.1 版本 服务器代码中的 Meteor.method({}) 不起作用
- Jquery 切换/单击在早于 9 的 Safari 版本中不起作用
- 尽管使用了最新的 angularJS 版本,但 ng-repeat-start 不起作用的任何原因
- jquery Animate 在 IE9 及更早版本中不起作用
- 上传 - HTML5 版本 文件扩展名不起作用
- 动态版本中的类不起作用
- <?=标签在代码点火器2.1.0版本中不起作用