我已經使用腳本中聲明的兩個腳本。每個腳本在單獨聲明時都能很好地工作,但如果我聲明第二個腳本。另一個功能另一個不行。衝突的腳本不能正常工作
以下是在單獨聲明並與其他腳本一起聲明的代碼。 這有他自己的js文件。
var fixed = false;
$(document).scroll(function() {
if($(this).scrollTop() > 20) {
if(!fixed) {
fixed = true;
$('.navbar-fixed-top').addClass('scroll');
}
} else {
if(fixed) {
fixed = false;
$('.navbar-fixed-top').removeClass('scroll');
}
}
});
/*
* Smooth Scroll
*/
$(function() {
function filterPath(string) {
return string
.replace(/^\//,'')
.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
.replace(/\/$/,'');
}
var locationPath = filterPath(location.pathname);
var scrollElem = scrollableElement('html', 'body');
// Any links with hash tags in them (can't do ^= because of fully qualified URL potential)
$('a[href*=#]').each(function() {
// Ensure it's a same-page link
var thisPath = filterPath(this.pathname) || locationPath;
if ( locationPath == thisPath
&& (location.hostname == this.hostname || !this.hostname)
&& this.hash.replace(/#/,'')) {
// Ensure target exists
var $target = $(this.hash), target = this.hash;
if (target) {
// Find location of target
var targetOffset = $target.offset().top;
$(this).click(function(event) {
// Prevent jump-down
event.preventDefault();
// Animate to target
$(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
// Set hash in URL after animation successful
location.hash = target;
});
});
}
}
});
// Use the first element that is "scrollable" (cross-browser fix?)
function scrollableElement(els) {
for (var i = 0, argLength = arguments.length; i <argLength; i++) {
var el = arguments[i],
$scrollElement = $(el);
if ($scrollElement.scrollTop()> 0) {
return el;
} else {
$scrollElement.scrollTop(1);
var isScrollable = $scrollElement.scrollTop()> 0;
$scrollElement.scrollTop(0);
if (isScrollable) {
return el;
}
}
}
return [];
}
});
另一個是在第一個腳本下方聲明的。現在當另一個存在時這不起作用,但是當單獨工作時是完美的。我也嘗試過沒有衝突的腳本,但結果相同。
<script>
$(document).ready(function(){
$(function(){
document.querySelector("#nav-toggle")
.addEventListener("click", function() {
this.classList.toggle("active");
$(".navmobile").slideToggle();
});
});
$(window).resize(function() {
if ($(window).width() >= 767) {
$(".navmobile").hide();
}
else {
$("#nav-toggle").removeClass("active");
$(".navmobile").hide();
}
});
});
</script>
*「另一個功能沒有。」*我不知道這意味着什麼。 – epascarello
腳本不能一起工作。 1工作,而另一個不工作。只有一個函數在聲明時才起作用。 – MIke
所以你需要弄清楚爲什麼。似乎兩個人都在錨上工作,所以我猜你需要排除其他人使用的錨。 – epascarello