I have been trying to achieve the effect at this link (hidden link) but on a div containing the post title as opposed to a menu. I have copied example code such as that at (hidden link) into my content template but I simply cannot get it to work.
Is this a limitation of Toolset or is there a specific way I need to implement this?
Any advise is most appreciated and a working example would be great as I haven't really used scripts before.
Hi, when working the Toolset environment, the jQuery object is represented by jQuery instead of $. For example, instead of this:
You should use this:
Replace every "$" symbol in your script code with "jQuery", then place that code in the JS panel of your Content Template, and try again. If you're running into problems after that, open up the browser console and see if any JavaScript errors are thrown. Let me know what you find out.
Ok, I see this has allowed the function to work, though I now wonder if the function is the wrong one. I had though the number set in the function defined how many pixels were scrolled before the function was activated, but it doesn't appear to be doing that. If you go to hidden link you will see the example I am testing on using the code below.
What I need is for the header to freeze before it passes under the menu rather than re-appearing afterward. Can you advise?
HTML...
<div class="row">
<div class="col-sm-4">
<header>
[types field='page-intro'][/types]
</header>
</div>
<div class="col-sm-6">
<!--<h1>[wpv-post-title]</h1>-->
<p>[wpv-post-body view_template="None"]</p>
[gallery columns="5" size="large" link="file" ids="[types field='gallery-1-images-x5'][/types]"]
</div>
</div>
CSS...
.fixed {
position: fixed;
top: 100px;
z-index: 1;
}
Script...
jQuery(document).ready(function(){
jQuery(window).bind('scroll', function() {
var navHeight = jQuery( window ).height() - 100;
if (jQuery(window).scrollTop() > navHeight) {
jQuery('header').addClass('fixed');
}
else {
jQuery('header').removeClass('fixed');
}
});
});
My guess is that this line isn't accurate:
var navHeight = jQuery( window ).height() - 100;
This navHeight variable is used to determine when the "fixed" class is applied to the header text. If it's applied too late, this would cause the text to scroll underneath the menu before snapping into place. That would seem to indicate that the variable is defined incorrectly. You need to calculate the height from the top of the menu to the top of the text. See the attached screenshot.
That's solved it - I guess I hadn't been adjusting the figure enough, thanks!