2016-05-09 9 views
0

Ich versuche diese funktion zu bekommen - aber es geht nicht das geht das $ (this) wenn es gefeuert wird. Ich fand diese Frage, was scheint, wirklich ähnliche Frage zu sein (How to use $(this) in functions?), aber ich kann nicht herausfinden, warum es in meinem Fall nicht funktioniert.

Es wäre toll, wenn mir jemand helfen könnte. Dank

function scrolll(){ 
 
     var thiss = $(this).attr('id').substring(1); 
 
     var pos = $(this).scrollTop(); 
 
     // var next = Number(thiss) + 1; 
 
     // var prev = Number(thiss) - 1; 
 

 
     count = Number(thiss); 
 

 
     if (pos == 0) { 
 
      $(".section#s" + (count - 1)).removeClass("inactive", 1500, "linear"); 
 
      $(this).removeClass("active", 1500, "linear"); 
 
      var count = Number(thiss) - 1; 
 
     } 
 

 
     if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) { 
 
      $(this).addClass("inactive", 1500, "linear"); 
 
      $(".section#s" + (count + 1)).addClass("active", 1500, "linear"); 
 
      var count = Number(thiss) + 1; 
 
      console.log("countbefore: " + count); 
 
      count++; 
 
      console.log("countafter: " + count); 
 
     } 
 
    } 
 

 

 

 

 
$('.section1').on('scroll', function() { 
 
     console.log(thiss) 
 
     scrolll($(this)); 
 
    }); 
 

 
$('.section2').on('scroll', function() { 
 
     console.log(thiss) 
 
     scrolll($(this)); 
 
    });

Antwort

0

einfachste Möglichkeit ist, einen Parameter zu scrolll und nicht stoßen sie Sorge hinzufügen, zum Beispiel:

function scrolll(el) 
{ 
    var $this = $(el) 
    var thiss = $this.attr('id').substring(1); 

    .... etc 

$('.section2').on('scroll', function() { 
    scrolll(this); 
}); 

Aber was Sie suchen call ist:

function scrolll() 
{ 
    .... 

$('.section2').on('scroll', function() { 
    scrolll.call(this); 
}); 
+0

vielen Dank, das war genau das, was ich gesucht habe. - Und es funktioniert. –

0

$(this) wird in einer Funktion in Zusammenhang mit dem Wähler in erster Linie verwendet.

z.

$('.button').click(function(){ 
$(this).css('color','red'); 
}); 

In diesem Fall $ (this) würde die .button Wähler Ziel.

+0

Das genau ist das Szenario es wird verwendet, aber möchte es an eine gemeinsame Funktion weitergeben. –