0

For a one-page layout website I would need the current section's heading to display in a fixed position on the left side of the screen in order to show the user where he currently is.

The heading will probably have an icon on its left side. I would like the floating heading to be only visible when the user is scrolling on the page, thus disappear when he halts, while the icon stays visible all the time. Is that possible?

physalis
  • 253
  • 4
  • 16

1 Answers1

0

This is possible, and most easily done in JQuery. In CSS using

position: fixed;

Will allow you to position a element, then it'll stick where it is on the screen, regardless of the user scrolling up and down the page. Using this JQuery code:

$(window).scroll(function(){
}

Will detect when the user scrolls then will run whatever is inside of the function. If you put this inside of the function:

$("#scrollerbar").css({"display": "block"});
clearTimeout($.data(this, 'scrollTimeout'));
$.data(this, 'scrollTimeout', setTimeout(function() {
    $("#scrollerbar").css({"display": "none"});        
}, 250));

With this HTML:

<div id="scrollerbar">You're scrolling</div>

And the following CSS:

#scrollerbar{
    display: none;
    position: fixed;
    top: 50px;
    left: 0;
}

This will create and position the div scrollerbar 50px down the page and set it to be invisible. Then whenever the user scrolls, it will set the scrollerbar to display: block (which makes it visible). Then stops the timeout "scrollTimeout" (if it's running). Then create the timeout "scrollTimeout" what will wait 250 milliseconds then set scrollerbar to invisible again.

Here's a JSFiddle: http://jsfiddle.net/Xanco/5q6oq8tm/

Please contact me if you have some questions.

EDIT

In order to answer the first part of your question, I've updated the JSFiddle: http://jsfiddle.net/Xanco/5q6oq8tm/2/

The scrollerbar contains a set of list items, each will be bound to a div:

<li id="1read">Reading 1</li>
<li id="2read">Reading 2</li>
<li id="3read">Reading 3</li>
<li id="4read">Reading 4</li>

There's also a new class name:

.active{
    font-weight: bold;
}

I've created a function that will remove all "active" class names from the list items in the scrollerbar:

function removeActive(){
    $("#1read").removeClass("active");
    $("#2read").removeClass("active");
    $("#3read").removeClass("active");
    $("#4read").removeClass("active");
}

And now whenever the user scrolls, JavaScript will check what the user is currently reading and add the "active" class to the appropriate list items in the scrollerbar:

if ($("#1").is(":hover")) {
    removeActive();
    $("#1read").addClass("active");
}
if ($("#2").is(":hover")) {
    removeActive();
    $("#2read").addClass("active");
}
if ($("#3").is(":hover")) {
    removeActive();
    $("#3read").addClass("active");
}
if ($("#4").is(":hover")) {
    removeActive();
    $("#4read").addClass("active");
}

EDIT 2

Now, the JavaScript will take the innerText of a child of the hovered div with the ID "articleheader" instead of using a non-standard attribute, as demonstrated by this line of code:

$("#displayaArticleName").text($(this).children("#articleheader")[0].innerText);

Here is the JSfiddle: http://jsfiddle.net/5q6oq8tm/6/

Please note that there are no set of attributes that can only be used, you may use custom attributes, as demonstrated here: http://jsfiddle.net/5q6oq8tm/4/

Xanco
  • 874
  • 1
  • 10
  • 15
  • Hi Xanco, thank you for your suggestion. It does the show-when-scrolling part really well. However, can you help out with the first part of my question, to put the current sections's heading into your #scrollerbar ;)? Thanks again ;). – physalis Aug 21 '14 at 11:31
  • Hi Physalis, I've updated my answer, and now it should have all the functionality you want. Let me know if you have any more questions. Thanks, Xanco :) – Xanco Aug 21 '14 at 12:43
  • Hey Xanco, oh yeah, it's quite close to what I need, hehe :). There's only one crucial feature: I'd like the sections have headings and make the script find and display it in a dynamic way, like: `

    First section

    Some content

    Second section

    Some other content

    ` and have the scrollerbar show the current position's section heading. Would that be possible, even best without having to stick it to static IDs (since this is gonna be incorporated into WordPress)? Thank you again for helping!!
    – physalis Aug 22 '14 at 11:08
  • You mean like this: http://jsfiddle.net/Xanco/5q6oq8tm/3/ ? If so, I'll update the answer to explain it all. – Xanco Aug 22 '14 at 11:29
  • Hey Xanco, thank you very much, getting much closer now. I hope you're not getting too impatient with me :). As I am planning to use it with WordPress and the sections' names have to be dynamic/changeable I cannot just hand-type it for every section into my JS, it's more like: 'Grab the heading of each section with the class="something" and put it out in a scrollbar.' Your version of course works in a static page, unfortunately it doesn't have the flexibility of a) dynamic headings, i.e. heading changes = scrollerbar title changes and b) scalability, i.e. WP can't add sections dynamically :). – physalis Aug 23 '14 at 12:19
  • What I mean is: Subpages (which appear as sections on the one-page layout) should be managed through WordPress as well as the decision to add more or deleted certain subpages. – physalis Aug 23 '14 at 12:22
  • @physalis Here's a updated version that will get the article titles dynamically: http://jsfiddle.net/5q6oq8tm/4/ – Xanco Aug 23 '14 at 13:58
  • Thank you, Xanco, that is exactly what I need :). Can you update your answer up there, so I can mark your answer as the solution? Only one question: Would it be possible to get rid of the extra attribute of the div and take the content of the h1 directly? I would suspect the page not to validate with an alien attribute like `articletitle`, would it? Thanks a bunch anyway, great one!! – physalis Aug 24 '14 at 22:12
  • Hey @Xanco, thanks again for your help, I finally came around to test it on the actual project & it finally worked out the way it should :). Only I had some problem with it, since it needs the exact child of an element when you use .children, so I basically changed it to .find so it could well be within a different structure. I put my changes into http://jsfiddle.net/5q6oq8tm/7/ in case some folks need it too ;). – physalis Sep 09 '14 at 10:25
  • And one more. Your code was still bugging me, since Firefox threw out an error of the likes `jQuery[]...find[] is undefined`. I found out it has something to do with innerText not being very well supported and should be replaced with `textContent` which all the browsers understand (see http://stackoverflow.com/questions/1359469/innertext-works-in-ie-but-not-in-firefox). I have put that into http://jsfiddle.net/5q6oq8tm/8/. Can you please update your post above to include that? – physalis Sep 09 '14 at 12:51