javascript
  • jquery
  • ajax
  • dom
  • 2016-07-23 10 views 0 likes 
    0

    Ye, ich weiß über "on" -Methode und die meisten Antworten hier über "on" sind nicht spezifisch. Kann mir jemand mit diesem Code helfen?Wie funktioniert das Jquery-Skript nach Ajax dom append?

    $(function(){ 
    
    var pagePositon = 0, 
        sectionsSeclector = 'article#sector', 
        $scrollItems = $(sectionsSeclector), 
        offsetTolorence = 30, 
        pageMaxPosition = $scrollItems.length - 1; 
    
    //Map the sections: 
    
    $scrollItems.each(function(index,ele) { $(ele).attr("debog",index).data("pos",index); }); 
    
    // Bind to scroll 
    $(window).bind('scroll',upPos); 
    
    //Move on click: 
    $(document).keypress(function(e) { 
        if (e.which == 100 && pagePositon+1 <= pageMaxPosition) { 
         pagePositon++; 
         $('html, body').stop().animate({ 
           scrollTop: $scrollItems.eq(pagePositon).offset().top 
         }, 300); 
        } 
        if (e.which == 97 && pagePositon-1 >= 0) { 
         pagePositon--; 
         $('html, body').stop().animate({ 
           scrollTop: $scrollItems.eq(pagePositon).offset().top 
          }, 300); 
         return false; 
        } 
    }); 
    
    //Update position func: 
    function upPos(){ 
        var fromTop = $(this).scrollTop(); 
        var $cur = null; 
        $scrollItems.each(function(index,ele){ 
         if ($(ele).offset().top < fromTop + offsetTolorence) $cur = $(ele); 
        }); 
        if ($cur != null && pagePositon != $cur.data('pos')) { 
         pagePositon = $cur.data('pos'); 
        }     
    } 
    }); 
    

    Wenn Sie die Taste „d“ oder „a“ geht es mit id = „Sektor“ nächsten oder zurück zu und das ist in Ordnung. Aber manchmal, nach Ajax Call und nach dem Anhängen neuer Artikel, kann Skript nicht zu ihnen bewegen. Ich weiß, dass sie nicht gebunden sind (nach dom refresh) und wie man dieses Skript nach Ajax dom ändern lässt?

    +1

    _Es geht zum nächsten oder vorherigen mit id = "Sektor" _Ids müssen ** einmalig sein ** – Andreas

    +0

    Sie müssen '$ scrollItems' aktualisieren. Ansonsten enthält es nur die zum Zeitpunkt der Zuweisung vorhandenen Elemente. – Andreas

    +0

    ich weiß darüber, also brauche ich ein offensichtliches Beispiel, Leute. Ich kann nicht einmal Google Beispiel, um mein Problem zu lösen. – MonkeyCoder

    Antwort

    0

    Gelöst:

    $(function(){ 
    
    var pagePositon = 0, 
        sectionsSeclector = 'article#sector', 
        $scrollItems = $(sectionsSeclector), 
        offsetTolorence = 30, 
        pageMaxPosition = $scrollItems.length - 1; 
    
    //Map the sections: 
    
    
    // Bind to scroll 
    
    
    //Move on click: 
    $(document).on('keypress', function(e) { 
        $scrollItems = $(sectionsSeclector); 
        pageMaxPosition = $scrollItems.length - 1; 
        $scrollItems.each(function(index,ele) { $(ele).attr("debog",index).data("pos",index); }); 
    
        $(window).bind('scroll',upPos); 
    
        if (e.which == 100 && pagePositon+1 <= pageMaxPosition) { 
         pagePositon++; 
         $('html, body').stop().animate({ 
           scrollTop: $scrollItems.eq(pagePositon).offset().top 
         }, 300); 
        } 
        if (e.which == 97 && pagePositon-1 >= 0) { 
         pagePositon--; 
         $('html, body').stop().animate({ 
           scrollTop: $scrollItems.eq(pagePositon).offset().top 
          }, 300); 
         return false; 
        } 
    }); 
    
    //Update position func: 
    function upPos(){ 
        var fromTop = $(this).scrollTop(); 
        var $cur = null; 
        $scrollItems.each(function(index,ele){ 
         if ($(ele).offset().top < fromTop + offsetTolorence) $cur = $(ele); 
        }); 
        if ($cur != null && pagePositon != $cur.data('pos')) { 
         pagePositon = $cur.data('pos'); 
        }     
    } 
    

    });

    Verwandte Themen