onscroll表thead fixed并将tbody td width应用于ad th jquery $(窗口)。滚动(函数(){ if($(this).scrollTop()>740) { //$(“.fixed div Attention”)。追加($header); var first=0; 变量长度=$('.table thead tr th').length; 对于(var i=0;i
如果您想注册一个只运行一次的侦听器,您需要删除函数本身中的侦听器。我将使用jQuery,因为在您的问题中使用了它 以您自己的代码为例:onscroll表thead fixed并将tbody td width应用于ad th jquery $(窗口)。滚动(函数(){ if($(this).scrollTop()>740) { //$(“.fixed div Attention”)。追加($header); var first=0; 变量长度=$('.table thead tr th').length; 对于(var i=0;i,jquery,Jquery,如果您想注册一个只运行一次的侦听器,您需要删除函数本身中的侦听器。我将使用jQuery,因为在您的问题中使用了它 以您自己的代码为例: $(window).scroll(function(){ if ($(this).scrollTop() > 740 ) { //$(".fixed-div-attendance").append($header); var first=0; var length=$('.table thea
$(window).scroll(function(){
if ($(this).scrollTop() > 740 )
{
//$(".fixed-div-attendance").append($header);
var first=0;
var length=$('.table thead tr th').length;
for (var i=0; i<length; i++)
{
console.log(i);
var thWidth=$(".table").find("th:eq("+i+")").width();
var tdWidth=$(".table").find("td:eq("+i+")").width();
if (thWidth<tdWidth)
{
if (i==0) {
$(".table thead").find("th:first").width(tdWidth-50);
}
else
{
$(".table thead").find("th:eq("+i+")").width(tdWidth);
}
}
else
{
$(".table").find("td:eq("+i+")").width(thWidth);
}
first=1;
}
$('.table').find('thead ').addClass('fixed-div-attendance');
//code
}
else {
//$('.table thead tr th:first').css('width','auto');
$('.table').find('thead ').removeClass('fixed-div-attendance');
}
});
$(窗口)。滚动(onWindowScroll);
函数onWindowScroll(){
if($(this).scrollTop()>740){
//$(“.fixed div Attention”)。追加($header);
var first=0;
变量长度=$('.table thead tr th').length;
对于(变量i=0;i
唯一的新部分是行$(window).off(“scroll”,onWindowScroll);
,我将回调移到它自己的函数
基本上,jQuery函数
.scroll(函数)
获取一个函数,并将其绑定到您调用它的任何对象的滚动事件。在这种情况下,您将其绑定到窗口
。由于该函数绑定到滚动
事件,并且现在有了自己的函数定义,因此您可以使用jQuery函数off
解除绑定。off只需使用事件,以及绑定到它的函数。在本例中,“scroll”
和onWindowsScroll
如果只想运行for循环一次,则需要一个全局变量来检查它是否已触发。看起来您的路径正确。下面是我的代码:
$(window).scroll(onWindowScroll);
function onWindowScroll() {
if ($(this).scrollTop() > 740) {
//$(".fixed-div-attendance").append($header);
var first = 0;
var length = $('.table thead tr th').length;
for (var i = 0; i < length; i++) {
console.log(i);
var thWidth = $(".table").find("th:eq(" + i + ")").width();
var tdWidth = $(".table").find("td:eq(" + i + ")").width();
if (thWidth < tdWidth) {
if (i == 0) {
$(".table thead").find("th:first").width(tdWidth - 50);
} else {
$(".table thead").find("th:eq(" + i + ")").width(tdWidth);
}
} else {
$(".table").find("td:eq(" + i + ")").width(thWidth);
}
first = 1;
}
$('.table').find('thead ').addClass('fixed-div-attendance');
//code
} else {
//$('.table thead tr th:first').css('width','auto');
$('.table').find('thead ').removeClass('fixed-div-attendance');
}
//This is the end of the on scroll function
$(window).off("scroll", onWindowScroll);
});
var first=0;//将它放在外部,这样它将是一个全局变量
$(窗口)。滚动(函数(){
if($(this).scrollTop()>740)
{
//$(“.fixed div Attention”)。追加($header);
如果(第一个==0){
myLovelyForLoop();
第一个=1;
}
$('.table').find('thead').addClass('fixed-div-attention');
//代码
}
否则{
//$('.table thead tr th:first').css('width','auto');
$('.table').find('thead').removeClass('fixed-div-attention');
}
});
函数myLovelyForLoop(){
$('.table thead tr th')。每个(函数(i){
var thWidth=$(this.width();
var tdWidth=$(“.table”).find(“td:eq(“+i+”)).width();
如果(thWidth)“如何最小化此代码”,这是什么意思?我想您可以删除方法末尾的侦听器。或者添加一个标志
var first=0; //put it outside, so it will be a global variable
$(window).scroll(function(){
if ($(this).scrollTop() > 740 )
{
//$(".fixed-div-attendance").append($header);
if(first == 0){
myLovelyForLoop();
first = 1;
}
$('.table').find('thead ').addClass('fixed-div-attendance');
//code
}
else {
//$('.table thead tr th:first').css('width','auto');
$('.table').find('thead ').removeClass('fixed-div-attendance');
}
});
function myLovelyForLoop() {
$('.table thead tr th').each(function(i){
var thWidth=$(this).width();
var tdWidth=$(".table").find("td:eq("+i+")").width();
if (thWidth<tdWidth){
if (i==0){
$(this).width(tdWidth-50);
}else{
$(this).width(tdWidth);
}
}else{
$(".table").find("td:eq("+i+")").width(thWidth);
}
});
}