gpt4 book ai didi

javascript - jQuery/js 在内部工作但不在外部工作

转载 作者:行者123 更新时间:2023-11-30 16:17:38 25 4
gpt4 key购买 nike

当我在 <script></script> 之间粘贴以下脚本时在 html 文档中它工作正常。

但是,当我通过 <script src="example.js></script> 加载它时它仅部分运行(第一部分运行但第二部分不运行)。

代码如下:

//SMOOTH SCROLL
$(function() {
$('a[href*="#"]:not([href="#"])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html, body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
// End of SMOOTH SCROLL

//NAV ENLARGE
$(window).on('scroll', function () {
var scrollTop = $(window).scrollTop();
if (scrollTop > 1) {
$('#main-navigation').stop().animate({height: "10vh"},100);
$("#main-navigation").css("position", "fixed");
$("#main-navigation").css("top", "0");
$('#main-navigation').css("width", "100vw");
}
else {
$('#main-navigation').stop().animate({height: "100vh"},100);
$("#main-navigation").css("position", "static");
}
});
//End NAV ENLGAR

最佳答案

将 jQuery 和您的外部 example.js 放入 head

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="/example.js"></script>
</head>

Example.js

$(function() {
$('a[href*="#"]:not([href="#"])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {

var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');

if (target.length) {
$('html, body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});

$(window).on('scroll', function () {
var scrollTop = $(window).scrollTop();
if (scrollTop > 1) {
$('#main-navigation').stop().animate({height: "10vh"},100);
$("#main-navigation").css("position", "fixed");
$("#main-navigation").css("top", "0");
$('#main-navigation').css("width", "100vw");
}
else {
$('#main-navigation').stop().animate({height: "100vh"},100);
$("#main-navigation").css("position", "static");
}
});
});

关于javascript - jQuery/js 在内部工作但不在外部工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35252741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com