Scrolling child div makes parent div scrolls - html

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);

Related

Scroll to Next Section jquery

(function ($) {
var window = $(window),
one = $("#one"),
two = $("#two"),
three = $("#three"),
four = $("#four"),
oneT = one.offset().top,
twoT = two.offset().top,
threeT = three.offset().top,
fourT = four.offset().top;
function Scroll(div) {
var tp = $(div).offset().top;
$("html, body").animate({ scrollTop: tp }, 500);
}
var tmp = 0;
var mousewheelevt = /Firefox/i.test(navigator.userAgent)
? "DOMMouseScroll"
: "mousewheel";
$("section").bind(mousewheelevt, function (e) {
var evt = window.event || e;
evt = evt.originalEvent ? evt.originalEvent : evt;
var delta = evt.detail ? evt.detail * -40 : evt.wheelDelta;
console.log(delta);
if (delta < 0) {
tmp++;
if (tmp > 0) {
var divT = $(this).next();
Scroll(divT);
tmp = 0;
}
} else if (delta > 0) {
tmp--;
console.log("going up");
if (tmp < -1) {
var divT = $(this).prev();
Scroll(divT);
tmp = 0;
}
}
});
})(jQuery);
This is the code im using is there any problem , i am getting error called
index.html:100 Uncaught TypeError: Cannot read properties of undefined (reading 'top')
Can you please help me with this.
You likely do not have 4 sections in your HTML or you have divs with a class and you need a dot: $(".section")
Then you need to use the wheel event instead of your current deprecated code
jQuery messes things up and you need to then use the originalEvent
You do not actually use any of the vars you declared in the beginning
I also got rid of half the tests by testing the existence of next/prev
(function($) {
function Scroll($div) {
var tp = $div.offset().top;
$("html, body").animate({
scrollTop: tp
}, 500);
}
const $sections = $("section");
$sections.on("wheel", function(e) {
const delta = e.originalEvent.wheelDelta; // all newer browsers
const down = delta < 0;
let $divT = down ? $(this).next("section") : $(this).prev("section");
// we may get a next or previous that is undefined - not obvious
if (!$divT.attr("id") || $divT.length === 0) {
if (down) $divT = $sections.first();
else $divT = $sections.last();
}
Scroll($divT);
});
})(jQuery);
section {
height: 500px;
border: 1px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<section id="one">One</section>
<section id="two">Two</section>
<section id="three">Three</section>
<section id="four">Four</section>

JQuery - delay function

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'});
}

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 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?

Snap draggable cloned element to <div> section

Can anyone check this sample code and can help me in fixing the clone for the dragged element and it should snap to the nearest white block when interchanging the positions
as without cloning it is snapping and I would like to how to do with a cloned element.
And if I am able to clone then i am unable to snap.So I will be grateful if anyone helps me out this issue.
Here is the sample code for snapping:
http://jsfiddle.net/kiran/qWPAH/7/
And this is how I did the cloning:
$(function() {
$(".draggable").draggable();
$(".item").droppable({
drop: function(event, ui) {
var $this = $(this);
$this.append(ui.draggable);
var width = $this.width();
var height = $this.height();
var cntrLeft = (width / 2) - (ui.draggable.width() / 2);
var cntrTop = (height / 2) - (ui.draggable.height() / 2);
ui.draggable.css({
left: cntrLeft + "px",
top: cntrTop + "px"
});
}
});
});
$(function() {
$(".draggable").draggable();
$(".item").droppable({
drop: function(event, ui) {
var $this = $(this);
$this.append(ui.draggable);
var width = $this.width();
var height = $this.height();
var cntrLeft = (width / 2) - (ui.draggable.width() / 2);
var cntrTop = (height / 2) - (ui.draggable.height() / 2);
ui.position.left = cntrLeft;
ui.position.top = cntrTop ;
}
});
});