我不知道是否可以这样做,就像我有 2 个 js 文件一样。第一个 Js 文件:
var news_pos, about_pos, services_pos, clients_pos;
function define_pos() {
var $top_slides=$('#top_slides'),
$mid_nav=$('#mid_nav'),
$news_section=$('#section-1');
var fixed_height = $top_slides.height() + $mid_nav.height();
news_pos = fixed_height - 20;
about_pos = fixed_height + $news_section.height();
services_pos = fixed_height + $news_section.height() * 2;
clients_pos = fixed_height + $news_section.height() * 3;
}
$(document).ready(function() {
var section_news = $('#section-1'),
section_about = $('#section-2'),
section_services = $('#section-3'),
section_clients = $('#section-4');
setheight();
function setheight() {
var section_height = $(window).height() + 200;
$section_news.height(section_height);
$section_about.height(section_height);
$section_services.height(section_height);
$section_clients.height(section_height);
define_pos();
}
});
第二个JS文件:
$(文档).ready(函数() {
var nav = { '$btn1': $('#btn1'), '$btn2': $('#btn2'), '$btn3': $('#btn3'), '$btn4': $('#btn4'), '$btn5': $('#btn5'), myclick : function() { myclicked(nav.$btn1, 0); myclicked(nav.$btn2, news_pos); myclicked(nav.$btn3, about_pos); myclicked(nav.$btn4, services_pos); myclicked(nav.$btn5, clients_pos); function myclicked(j,k) { j.click(function(e) { e.preventDefault(); $('html,body').animate({scrollTop: k}, 1000); }); } // Is it right to do return{'myclick':myclick}, // how to call? seems not logical } }; // Here will not work because it say news_pos is undefined. // If I use setTimeout(nav.myclick,1000), it will works but // I want to run it right the when position is caculated. nav.myclick();
});
如何将 nav.myclick() 函数传递给第一个 js 文件并将其放入 setheight() 和 define_pos() 下?顺便说一句,在 stackoverflow 中编写代码很奇怪,按 tab 并没有真正给你任何间距。