JQuery - delay function - html

I have a function that positions a side nav to a fixed position depending on other elements (including a fixed header) on scroll/resize/load. The position top is calculated next to the content container (#timelineFulltext) OR below a header (.sticky).
The problem is the position is kicking in before the sticky header hides, which is another function.
Is there a way I can delay the execution of this function:
jQuery(window).on('load scroll resize', function (){
// Position TOC on load, scroll and resize
var isSticky = jQuery('.sticky'); // Check if sticky header is present
if(isSticky.length && jQuery('#timelineFulltext').offset().top < jQuery(window).scrollTop()){
var timelinePosition = isSticky.height() + 38;
}
else{
var timelinePosition = jQuery('#timelineFulltext').offset().top - jQuery(window).scrollTop();
}
jQuery('.fixed-timeline').css({'top': timelinePosition + 'px'});
});

A solution is to use setTimeOut. In this example it will wait 3 seconds before executing your function.
jQuery(window).on('load scroll resize',function() {
setTimeout(function() {
// Position TOC on load, scroll and resize
var isSticky = jQuery('.sticky'); // Check if sticky header is present
if(isSticky.length && jQuery('#timelineFulltext').offset().top < jQuery(window).scrollTop()){
var timelinePosition = isSticky.height() + 38;
}
else{
var timelinePosition = jQuery('#timelineFulltext').offset().top - jQuery(window).scrollTop();
}
jQuery('.fixed-timeline').css({'top': timelinePosition + 'px'});
},
3000);
});

you can create a function say hideStickyBar to apply header sticky bar script. Call this function as callback from hide function in jquery. See below sample code
$('#elementTohide').hide(400, hideStickyBar);
jQuery(window).on('load scroll resize', function (){
hideStickyBar();
});
function hideStickyBar() {
// Position TOC on load, scroll and resize
var isSticky = jQuery('.sticky'); // Check if sticky header is present
if(isSticky.length && jQuery('#timelineFulltext').offset().top < jQuery(window).scrollTop()){
var timelinePosition = isSticky.height() + 38;
}
else{
var timelinePosition = jQuery('#timelineFulltext').offset().top - jQuery(window).scrollTop();
}
jQuery('.fixed-timeline').css({'top': timelinePosition + 'px'});
}

Related

Flutter webview has white space after using webkitColumnGap css

I used following css so html can be horizontally scrolled,
bodyElement.style.webkitColumnGap = paddingLeft + paddingRight + "px";
bodyElement.style.webkitColumnWidth = pageWidth + "px";
bodyElement.style.columnFill = "auto";
bodyElement.style.margin = 0;
htmlElement.style.height = pageHeight + (paddingTop + paddingBottom) + "px";
bodyElement.style.height = pageHeight + "px";
Some of the values,
padding = 20, 20, 20, 20
window.innerWidth = 412
window.innerHeight = 604
clientWidth = 412
clientHeight = 604
bodyElement.offsetWidth = 412
bodyElement.offsetHeight = 8062
pageWidth = 372
pageHeight = 564
document.documentElement.offsetWidth = 412
Everything works fine html can be scrolled horizontally but I see lot of white-space in page showed in above image. Which gives vertical scroll bar. So on touch you can scroll vertically.
My first thought was to do the workaround just disable the vertical scrolling by using following css,
bodyElement.style.overflow = "hidden"; // didn't worked
Second thought was to add onscroll listener and return without scrolling also didn't worked,
htmlElement.onscroll = (e) => {
Toaster.postMessage("-> html scroll event -> go back = ");
e.preventDefault();
};
bodyElement.onscroll = (e) => {
Toaster.postMessage("-> body scroll event -> go back = ");
e.preventDefault();
};
window.onscroll = (e) => {
Toaster.postMessage("-> window scroll event -> go back = ");
e.preventDefault();
};
I have run out of ideas so would like help from you guys.
Thanks in advance.

I want to make a sidebar like google play store by css and js

I want to make a sidebar like gool
e play store..When i scroll left side in the body then the sidebar should appear...
Is there any idea to make such a sidebar?
For js.
$(function () {
var msie6 = $.browser == 'msie' && $.browser.version < 7;
if (!msie6 && $('#sidebar').offset()!=null) {
var top = $('#sidebar').offset().top - parseFloat($('#sidebar').css('margin-top').replace(/auto/, 0));
var height = $('#sidebar').height();
var footerTop = $('#footer').offset().top - parseFloat($('#footer').css('margin-top').replace(/auto/, 0));
var gap = 15;
scrollingSideBar(top,height,footerTop,gap);
$(window).resize(function (event) {
scrollingSideBar(top,height,footerTop,gap);
});
$(window).scroll(function (event) {
scrollingSideBar(top,height,footerTop,gap);
});
}
});
function scrollingSideBar(top,height,footerTop,gap){
var winHeight = $(window).height();
var winWidth = $(window).width();
var y = $(this).scrollTop();
var x = $(this).scrollLeft();
if (height+top+gap < winHeight) {
$('#sidebar').css({ position: "fixed",top: top, left:-x});
} else if(y+winHeight > height+top+gap){
$('#sidebar').css({position: "fixed",top: winHeight-height-gap, left:-x});
} else if (y+winHeight > footerTop) {
$('#sidebar').css({position: "fixed",top: footerTop-height-y-gap, left:-x});
} else {
$('#sidebar').css({position: "absolute",top: top, left:-x/winWidth});
}
}
For CSS you just right click on browser and inspect it.

How to use Mouse Wheel for jumping to next or previous # section in html

I have a site with top nav menu:
<nav id="menu">
<ul id="menu-nav">
<li class="current">Home</li>
<li>Cakes</li>
<li>Candy</li>
<li>Marshmellow</li>
<li>Honey</li>
</ul>
</nav>
And all my sections are:
<div id="Cakes" class="page">
<div class="container">
This is sweet
</div>
</div>
How can I use the mouse wheel to jump directly to the next or previous section?
Currently the mouse wheel will scroll the page like normal. But I want it to jump to every section with one wheel rotation without scrolling all the entire page.
Without plugins, all we need to do is to capture the mousewheel using mousewheel event -on Firefox we use DOMMouseScroll instead- and depending on the value of the event's originalEvent.wheelDelta -again in Firefox it is originalEvent.detail, thanks Firefox- if this value is positive then the user is scrolling upward, if it's negative then the direction is down.
JS Fiddle 1
jQuery (1) :
//initialize
var winHeight = $(window).height(),
pages = $('.page'),
navLinks = $('#menu-nav a'),
currentPage = 0;
$('html, body').animate({ scrollTop: 0}, 0);
// listen to the mousewheel scroll
$(window).on('mousewheel DOMMouseScroll', function(e){
//by default set the direction to DOWN
var direction = 'down',
$th = $(this),
// depending on the currentPage value we determine the page offset
currentPageOffset = currentPage * winHeight;
// if the value of these properties of the even is positive then the direction is UP
if (e.originalEvent.wheelDelta > 0 || e.originalEvent.detail < 0) {
direction = 'up';
}
// if the direction is DOWN and the currentPage increasing won't exceed
// the number of PAGES divs, then we scroll downward and increase the value
// of currentPage for further calculations.
if(direction == 'down' && currentPage <= pages.length - 2){
$th.scrollTop(currentPageOffset + winHeight);
currentPage++;
} else if(direction == 'up' && currentPage >= 0) {
// else scroll up and decrease the value of currentPage IF the direction
// is UP and we're not on the very first slide
$th.scrollTop(currentPageOffset - winHeight);
currentPage--;
}
});
// as final step we need to update the value of currenPage upon the clicking of the
// navbar links to insure having correct value of currentPage
navLinks.each(function(index){
$(this).on('click', function(){
navLinks.parent().removeClass('current');
$(this).parent().addClass('current');
currentPage = index;
});
});
(1) UPDATE
If you don't want to use jQuery, below is pure javascript code doing the same as above, this won't work in IE8 and below though:
JS Fiddle 2
Pure Javascript:
//initialize
var winHeight = window.innerHeight,
pages = document.getElementsByClassName('page'),
navLinks = document.querySelectorAll('#menu-nav a'),
currentPage = 0;
window.addEventListener('mousewheel', function(e) {
scrollPages(e.wheelDelta);
});
window.addEventListener('DOMMouseScroll', function(e) {
scrollPages(-1 * e.detail);
});
function scrollPages(delta) {
var direction = (delta > 0) ? 'up' : 'down',
currentPageOffset = currentPage * winHeight;
if (direction == 'down' && currentPage <= pages.length - 2) {
window.scrollTo(0, currentPageOffset + winHeight);
currentPage++;
} else if (direction == 'up' && currentPage > 0) {
window.scrollTo(0, currentPageOffset - winHeight);
currentPage--;
}
}
for (var i = 0; i < navLinks.length; i++) {
navLinks[i].addEventListener('click', updateNav(i));
}
function updateNav(i) {
return function() {
for (var j = 0; j < navLinks.length; j++) {
navLinks[j].parentNode.classList.remove('current');
}
navLinks[i].parentNode.classList.add('current');
currentPage = i;
}
}
You'll need to get a plug-in to add the mousewheel event.
Maybe try this one this.
it's got pretty good examples.

Scrolling child div makes parent div scrolls

I have the following test page:
(didn't post code 'cause would be too big)
As noticed, when you finish scrolling the little div in the bottom, the main div also scrolls. Is there anything, propriety to disable this behaviour?
Thanks.
See the plugin mentioned on this question:
Prevent scrolling of parent element?
Or take a look at the JS here (not my code). This code adds classes to the children and traps the scroll event which can be used to prevent the parent from scrolling.
http://codepen.io/LelandKwong/pen/edAmn
var trapScroll;
(function($){
trapScroll = function(opt){
var trapElement;
var scrollableDist;
var trapClassName = 'trapScroll-enabled';
var trapSelector = '.trapScroll';
var trapWheel = function(e){
if (!$('body').hasClass(trapClassName)) {
return;
} else {
var curScrollPos = trapElement.scrollTop();
var wheelEvent = e.originalEvent;
var dY = wheelEvent.deltaY;
// only trap events once we've scrolled to the end
// or beginning
if ((dY>0 && curScrollPos >= scrollableDist) ||
(dY<0 && curScrollPos <= 0)) {
opt.onScrollEnd();
return false;
}
}
}
$(document)
.on('wheel', trapWheel)
.on('mouseleave', trapSelector, function(){
$('body').removeClass(trapClassName);
})
.on('mouseenter', trapSelector, function(){
trapElement = $(this);
var containerHeight = trapElement.outerHeight();
var contentHeight = trapElement[0].scrollHeight; // height of scrollable content
scrollableDist = contentHeight - containerHeight;
if (contentHeight>containerHeight)
$('body').addClass(trapClassName);
});
}
})($);
var preventedCount = 0;
var showEventPreventedMsg = function(){
$('#mousewheel-prevented').stop().animate({opacity: 1}, 'fast');
}
var hideEventPreventedMsg = function(){
$('#mousewheel-prevented').stop().animate({opacity: 0}, 'fast');
}
var addPreventedCount = function(){
$('#prevented-count').html('prevented <small>x</small>' + preventedCount++);
}
trapScroll({ onScrollEnd: addPreventedCount });
$('.trapScroll')
.on('mouseenter', showEventPreventedMsg)
.on('mouseleave', hideEventPreventedMsg);
$('[id*="parent"]').scrollTop(100);

how to calculate browser body width and height

how can i calculate body width and height for all browser in java query...
$(document).ready(function () {
$(document).ready(sizeContent01);
function sizeContent01() {
var totHeight = ($("body").height());
var hdrHeight = $("#header").outerHeight(true);
var ftrHeight = $("#footer").outerHeight(true);
var bdyHeight1 = totHeight - hdrHeight - 10;
var outputHeight = bdyHeight1 - $(".input_content").outerHeight(true) - 10;
$("#body").css("height", bdyHeight1);
$(".output_content").css("height", (outputHeight - 5));
};
$(window).resize(sizeContent02);
function sizeContent02() {
var totHeight = ($("body").height());
var hdrHeight = $("#header").outerHeight(true);
var ftrHeight = $("#footer").outerHeight(true);
var bdyHeight1 = totHeight - hdrHeight - 10;
var outputHeight = bdyHeight1 - $(".input_content").outerHeight(true) - 10;
$("#body").css('height', bdyHeight1);
$(".output_content").css('height', (outputHeight - 5));
};
});
please any one help me....
In jQuery : $( window ).height();
and in pure js : window.innerHeight;
Atleast ive done godly with these. Then if you want the height off whole html document (includes scrolling) you can use this with jQuery : $( document ).height();
$(window).height();
$(window).width();
More info
http://api.jquery.com/height/
http://api.jquery.com/width/
If you think the scroll bar will be a problem you can try hiding them then taking your measurement
document.body.style.overflow = "hidden";
var viewportWidth = $(window).width();
var viewportHeight = $(window).height();
document.body.style.overflow = "";
Duplicate of Get the height and width of the browser viewport without scrollbars using jquery?