1

I have a feeling what ever gave me problems that I tried to find a solution in this question - Can't trigger a jquery function with scroll on a particular div - might be responsible for scroll related issues here.

Short version: Can't get this, or anything similar, to work

$("#Container3").scrollTop(0);

Nothing happens really, no error in the console, no wierd behaviour, just seems to ignore the scrollTop(0) request.

Long version: I'm sorry but posting a code snippet isn't feasible as it's a complex app-like interface but I'll try to explain the issue to the best of my abilities:

  • Mobile responsive website that loads different interfaces depending on screen real-estate.
  • Smallest interface composed of 3 parts - navigation at the top, search at the bottom and content in the middle.
  • Content is mostly loaded during use and not at page load.
  • At the push of a button that re-loads the contents of a particular div I also need to scroll that div to the top for usability purposes.
  • While it doesn't seem to influence my problem (removing it doesn't solve the issue) I should disclose that I'm using hammer.js to simulate touch events as it might influence the solution.

The load is done outside the viewport so animations aren't needed but I'll take them as long as they get this to work.

This is what my jquery request looks like

$(document).on("click",".NavRowButton",function(event){
    $("#Container3").scrollTop(0);
    var $targetButtonId=$(event.target).attr("id");
    $("#Content").load("/load/login/"+$targetButtonId+".php");
    $("#DisplayContentName").html("<span class='NavColSpan'>"+$targetButtonId+"</span>");
    $("#Container3").find(".WindowBorder").css("top","0");
});

#Container3 has the scroll bar and is the immediate parent of #Content.

This is a function I'm still building and is the solution for the problem I had before and also what I'm using now to help debugging this one:

document.addEventListener('scroll',function(event){
    if(event.target.className==='Container'){
        var $currentScroll=$(event.target).scrollTop();
        console.log($currentScroll);
        var $targetId=$(event.target).attr("id");
        console.log($targetId);
    }
},true);

Thanks in advance.

Edit: I just noticed that if I put a $(event.target).scrollTop(0); at the end of the scroll distance debugging function it actually resets the scroll so it seems that as long as the div is the event.target it works while from the outside as during the click function I might not selecting it appropriately.

Edit2: I just learned I can cache event.targets into variables and with a .get() inside the click function I'm sure I'm selecting the right element so it just leaves how the scrollTop(0) method works.

They now look like this(also had to add a condition to limit load events):

global variable:

$DivTestVar="";

click:

$(document).on("click",".NavRowButton",function(event){
    var $targetButtonId=event.target.id;
    if($targetButtonId != $("#DisplayContentName").html()){
        $($DivTestVar).scrollTop(0);
        console.log($($DivTestVar).get());
        $("#Content").load("/load/login/"+$targetButtonId+".php");
        $("#DisplayContentName").html($targetButtonId);
        $("#Container3").find(".WindowBorder").css("top","0");
    };
});

scroll debugging:

document.addEventListener('scroll',function(event){
    if(event.target.className==='Container'){
        $DivTestVar=event.target;
        var $currentScroll=$($DivTestVar).scrollTop();
        console.log($currentScroll);
        var $targetId=event.target.id;
        console.log($targetId);
    }
},true);

If I click before scrolling the console.log($($DivTestVar).get()); returns empty but if at the first scroll it starts returning the correct DOM element. The scrollTop(0) is ignored anyway.

Edit3: I just want to leave a small update. I have since given up on the method I was trying to use here for something with a similar effect but not as user friendly as what I was trying to achieve. As such I no longer care about this personally but if you're reading this and have a similar problem I have come across this issue a couple more times to a smaller effect and I now think it's related to position:fixed; elements and how scrollTop() deals with that but I don't have the time to delve into it more so good luck and godspeed.

Community
  • 1
  • 1
Ricardo Marques
  • 99
  • 1
  • 2
  • 10
  • Would love to see a jsfiddle demonstrating the problem. – davidkonrad Oct 05 '15 at 15:33
  • 1
    Flagging for: *Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself.* - Please [edit] your question to include a JS/HTML/CSS snippet that reproduces the error (with the least amount of code). See [mcve]. – BSMP Oct 05 '15 at 15:34
  • "doesn't work" isn't very descriptive. Describe _precisely_ what happens and how it differs from your hopes and dreams! – Lightness Races in Orbit Oct 05 '15 at 15:46
  • @davidkonrad, sorry I should have mentioned again that posting code isn't feasible because it's such a complex interface I have no idea what might be causing the problem. Someone was able to pinpoint a solution for my other problem without much info (probably happened to them too) so I was hoping someone would be able to help here as well. In any case I'll edit the question with more details i had on the other question. – Ricardo Marques Oct 05 '15 at 15:47
  • @LightnessRacesinOrbit Ups yeah I'll edit that into the question too. Nothing happens really. Nothing shows up on the console. Nothing, same as my other problem, it just doesn't really work :| – Ricardo Marques Oct 05 '15 at 15:48
  • At least explain what you're trying to do. We can't divine that from code that, by your own admission, doesn't achieve that goal! – Lightness Races in Orbit Oct 05 '15 at 15:48
  • @LightnessRacesinOrbit I'm going to re-write the question now, but I don't think I can explain what I'm trying to to in a simpler way, I need to reset the scrollTop() value to 0 at the click of a button that also re-loads the content of said div. – Ricardo Marques Oct 05 '15 at 16:04

2 Answers2

4

Did you try the pure JS version ?

document.getElementById('Container').scrollTop = 0
Gael
  • 167
  • 6
1

You have two possibilities, as far as I know.

1-Scroll the whole page until it reached the top of your #Content div position with jQuery.

2-Your #Content is inside a div with scroll, which scrollTop(0) will work for that (example: http://jsfiddle.net/zkp07abu/).

Zander
  • 1,076
  • 1
  • 9
  • 23
  • Unfortunately I can't scroll the whole page because I'm going to have more than 1 scrollable div's. I'm also pretty sure I'm selecting the right div because I'm actually console.logging the scrollTop() value of that same div in real time to help debugging another function. I just figured I should take care of this now before continuing. – Ricardo Marques Oct 05 '15 at 15:55
  • another way you could try, is if it's scrolled after clicking a button, you can find the parent, or sibling "#Content", get the top according to the body/html and put the scrollTop equal to that variable. – Zander Oct 05 '15 at 15:58
  • I added a brief description of what It looks like to the question. Nothing on the #Content tree scrolls besides it so I can only control the scroll on #Container3. Setting the scrollTop to 0 is the only way out, If I can't do that I'll have to change how I'm looking at this part of the interface. – Ricardo Marques Oct 05 '15 at 16:26
  • Correct me if I'm wrong. What you need is that #Content scrolls to the top once you have clicked a button. Now I see you have added a #Content3 that contains #Content. So, as the example given in point two, wouldn't help with what you are trying to achieve? – Zander Oct 07 '15 at 12:19
  • For some reason no... As I added on my 2nd edit I'm 100% sure I'm targeting the correct div as humanly possible so the only thing left is how the scrollTop method works. If I add a $($DivTestVar).scrollTop(0); to the end of the scrolling debugging function It works and it will lock the scrollTop value at what I tell it to, but outside of that function I simply can't get it to work. With the .get() method I'm 100% sure I'm coming up on a scrollTop limitation but I don't know what it is so I can fix it... – Ricardo Marques Oct 07 '15 at 13:25
  • Ok now I'm even more confused, I can't access the scrollTop outside of that scroll EventListener. I tried changing it to a click event and it stopped working... What the hell is happening... I'm way over my league here... Damn it. – Ricardo Marques Oct 07 '15 at 13:50