0

I have added 2 codes here the window.scroll works on my example but not the second one binding the div to the scroll.

Any one knows what am I doing wrong!?

Just so you know I'm working in MeteorJS <- I dont think that this is the problem bc. the window scrolling works.

This 2 codes are in the same js file.

    $(window).scroll(function() {
        lastSession = Session.get('c_info')[Session.get('c_info').current]
        if(lastSession.list == 0 && $(window).height() + $(window).scrollTop() >= $(document).height()){
            lastItem = $( ".list-item div:last" ).html();
            if (lastSession.page == 1){
                currentSession().more();
                lastItem2 = $( ".list-item div:last" ).html();
            } else if( lastItem2 != lastItem) {
                currentSession().more();
                lastItem2 = $( ".list-item div:last" ).html()
            }
        }
    });

    $('#playlist').bind('scroll',function() {
        console.log("div is scrolling");
    });

I tried this too:

 $('#playlist').scroll(function() {
    console.log("div is scrolling");
 });

MeteorJS Template:

<template name="playList">
    <div id="playlist" class="playlist show-for-large-up">
        {{#each list}}
        <a href="/video/{{_id}}" class="large-12 columns" id="pl{{v_id}}">
            <div>
                <div class="large-7 columns plRight">
                    <span>{{vTitle}}</span>
                </div>
            </div>
        </a>
        {{/each}}
    </div>
</template>

Also Tried:

$('#playlist').on('scroll',function() {console.log('test')});// not working

Tried to Change the id name and putting on the document ready:

$( document ).ready(function (){
        $('#pl_list').bind('scroll',function() {
                console.log("div is scrolling");
            });
    })//failed

The div has a scrollbar and the list is long and i have a css like this:

.playlist {
  padding: 0;
  overflow-y: scroll;
  height: 458px;
}

Also tried:

Template.playList.rendered = function () {
    console.log("playlist rendered");// i can see this on logs this tells that template is in doom
    Meteor.setTimeout(function(){
       $('#playlist').on('scroll',function(){
       console.log('Scrolling...');
    });
    }, 2000);// with settimeout i have giveng it 2 more seconds
}
em.rexhepi
  • 289
  • 1
  • 5
  • 17
  • can you reproduce in demo fiddle? – Ehsan Sajjad Nov 06 '14 at 14:20
  • 2
    Wow ! that is some very old jQuery you're using (> 3 years old...). As of v1.7, `on()` replaced the `bind()` method. That said, most issues of that type are due either to the selector either to the fact that the element is dynamically generated and you need to use delegated events. – Laurent S. Nov 06 '14 at 14:22
  • @Bartdude i tried $('#playlist').on('scroll',function() {}); – em.rexhepi Nov 06 '14 at 14:25
  • @A.Wolff I have no duplicat id-s i will post now my template – em.rexhepi Nov 06 '14 at 14:25
  • @EhsanSajjad I'm afraid no. I'm using MeteorJS not just plain html. – em.rexhepi Nov 06 '14 at 14:27
  • Have you just try to set binding inside document ready handler? – A. Wolff Nov 06 '14 at 14:30
  • @A.Wolff yes sir I tried that too. Btw $(window).scroll(function() {}) without being in a document ready I assume this should work to without being on document ready. – em.rexhepi Nov 06 '14 at 14:31
  • No window is available before the DOM unlike any element inside it. So try to set you binding for the DIV inside document ready or just before BODY closing tag, e.g: `$(function(){$('#playlist').scroll(function() {...});});` – A. Wolff Nov 06 '14 at 14:33
  • Is your content generated by javascript or not ? I don't know meteor... if yes, you will need to use delagated events (cfr jQuery doc) and attach the event to the closest static parent. – Laurent S. Nov 06 '14 at 14:34
  • @Bartdude scroll event doesn't bubble AFAIK but still be captured on modern browsers, i guess – A. Wolff Nov 06 '14 at 14:34
  • Typo regarding `#pl_list`, right? This is how you capture event instead, but doesn't work on IE8< http://jsfiddle.net/0kdjg3q8/ Basically, you should delegate event instead but onscroll doesn't bubble, so you cannot use delegation here. FYI, i don't know meteor.js, i'm quite sure it fire some event when new content is load in DOM, you should bind your scroll event from there – A. Wolff Nov 06 '14 at 14:38
  • @Bartdude yes it is generated from javascript. I have other javascript code working fine in this project except that one. ex. $('#playlist').animate({ scrollTop: $("#pl" + video_id).offset().top },'slow');// this works just fine – em.rexhepi Nov 06 '14 at 14:39
  • @user1898399 just for info, is it really the DIV which is scrolled, not the BODY? – A. Wolff Nov 06 '14 at 14:45
  • @A.Wolff yes i'm just trying to detect the div if it is scrolled. i have a list with like 10 divs and it has a scroll bar with a css like this .playlist { padding: 0; overflow-y: scroll; height: 458px; } – em.rexhepi Nov 06 '14 at 14:47
  • But IDs must be unique on document context, i hope you know that. Now how do you render content, maybe this is what you need: http://stackoverflow.com/questions/10109788/callback-after-the-dom-was-updated-in-meteor-js You should check meteorjs DOC – A. Wolff Nov 06 '14 at 14:48
  • @A.Wolff i mean i hav a div with id #playlist nad within that div i have content that is scrollable – em.rexhepi Nov 06 '14 at 14:51
  • @user1898399 So you have to bind scroll event on that specific scrollable elements, not the container, onscroll doesn't bubble. In fact, i'm really not sure to understand what you meant, provide a concrete example should help to find your issue, what about jsFiddle?! One time you use `#pl_list` so hard to figure it out – A. Wolff Nov 06 '14 at 14:52
  • Yes I'm trying to detect that specific div when is scrolling and console.log("something"); – em.rexhepi Nov 06 '14 at 14:54
  • @A.Wolff but $('#playlist').animate({ scrollTop: $("#pl" + video_id).offset().top },'slow'); works pritty well it reacts with #playlist – em.rexhepi Nov 06 '14 at 14:56
  • Could you use http://meteorpad.com/ to reproduce your issue?! – A. Wolff Nov 06 '14 at 14:57
  • @A.Wolff can you look at the code i added and it still is not working. And it will take time to put it on meteorpad.com – em.rexhepi Nov 06 '14 at 15:04
  • @user1898399 I'm sorry, cannot help you more. I hope a meteorjs pro will pass here to help you – A. Wolff Nov 06 '14 at 15:08
  • @A.Wolff i really appreciate your help. Thank you very much. If I could i would give you points, guys. Thank you. And hope that somebody will look at this. – em.rexhepi Nov 06 '14 at 15:10
  • @A.Wolff your code worked. I tried and RESET the meteorjs project. And it automagicly worked. If you are interested on what worked I have put an answer to my question. I'm very sorry and thankful for your time. – em.rexhepi Nov 06 '14 at 15:24

4 Answers4

2

Try this out -

$(document).ready(function(){
  $('#playlist').on('scroll',function(){
    console.log('Scrolling...');
  });
});
1

Use

 $('#playlist').scroll(function() {
    console.log("div is scrolling");
 });

instead (like you did for window).

Thats the purpose of scroll(). See jquery documentation.

jp-jee
  • 1,502
  • 3
  • 16
  • 21
1

Scrolling event is fired on the element, if it has scrolled. So if you only scrolling the "body" element of the DOM it will not be triggered for #playlist. So you have put a scrollbar to the container element of #playlist. Shot answer, cut the height and add a scrollbar, then the event will fire on it.

I did a Jsfiddle http://jsfiddle.net/34j0qnpg/4/

html
<div id="playlist-wrapper">
<div id="playlist" class="playlist show-for-large-up">
    <a href="/video/1" class="large-12 columns" id="pl1">
        <div>
            <div class="large-7 columns plRight">
                <span>Titel</span>
            </div>
        </div>
    </a>

css part

body, html {
padding: 0;
margin: 0;
background-color: lightgrey;
color: #fff;
font-family: Arial;
height: 5000px;
overflow-y:scroll;
}

#stats {
  position: relative;
}

#playlist-wrapper {
    border: 1px solid #000;
    padding: 10px;
    height: 300px;
    overflow-y: scroll;
}

#playlist {
    height: 1000px;
    background-color: darkgrey;
}

var $stats = $('#stats');
$('#playlist-wrapper').on('scroll', function() {
    $stats.html('playlist scrolling');
    console.log('playlist scrolling');
});

$(window).on('scroll', function() {
    $stats.html('window scrolling');
    console.log('window scrolling');
});
Robert
  • 664
  • 9
  • 25
  • I see this is working but not on meteor.js Can not understand why. Everything else with jquery is working but not this one. Thank you very much. – em.rexhepi Nov 06 '14 at 14:45
  • btw i have a scrollbar .playlist { padding: 0; overflow-y: scroll; height: 458px; } – em.rexhepi Nov 06 '14 at 14:45
  • What browser do are you testing? Did you tried another one, I had a some trouble with FF plugins recently. – Robert Nov 06 '14 at 14:50
  • Need more code input ... what jquery version, or is it Zepto or something else. Is $('#playlist').length > 0? – Robert Nov 06 '14 at 15:12
  • I just solved it by RESETING the meteorjs I dont know why but it just automagicly worked. – em.rexhepi Nov 06 '14 at 15:20
1

Solved with this code: Tried it earlyer no results, after meteorjs project reset it just automagicly workded:

Template.playList.rendered = function () {
  console.log("playlist rendered");
  $('#playlist').on('scroll',function(){
    console.log('Scrolling...');
  });
}

I answered my question just if anybody is searching for the same answer.

Thanks to anybody who tried to help me.

I LOVE THIS COMMUNITY.

em.rexhepi
  • 289
  • 1
  • 5
  • 17