I want to pre append some text when the user goes to top of contents of div, but I am facing one issue if there is no scrollable data in div. How to get that event (if user swipes or drags the unscrollable data)?
I will explain again.
http://jsfiddle.net/d9584/13/
in this fiddle data is scrollable (when user scroll to top it pre append the text in span).
same functionality does not work when there is no scrollable data
http://jsfiddle.net/d9584/14/
can we get an event if the user drags or scrolls the div that content (if there is no scrollable div contend).
I want to use some use some concept if there scrollable content it call this function
$("#contend").scroll(function () {
if ($(this).scrollTop() === 0) {
$("#pre").prepend("naveen ");
}
});
if there is no scrollable call another
Yo can do it like:
with scroll: DEMO
without scroll by click: DEMO
We check if the div has not scroll-y by this condition: if($(this)[0].scrollHeight <= $(this).height()). so if it doesn't have. We append by click.
$("#contend").click(function () {
if ( $(this)[0].scrollHeight <= $(this).height() ) {
$("#pre").prepend("naveen ");
}
});
$("#contend").scroll(function () {
if ($(this).scrollTop() === 0) {
$("#pre").prepend("naveen ");
}
});
Related
I have this problem I can't wrap my head around:
I am checking if the user is scrolling the page after using the search form. In which case, the search form should unfocus, with:
var content = document.querySelector('.content');
content.addEventListener("scroll", function(e) {
$("#search_box").blur();
});
Now, I also want the page to always scroll the content to the top as the user is typing, with:
$('#search_box').keyup(function() {
$('.content').animate({
scrollTop: 0
});
}
As you can see, this creates a problem. The user is typing, the page scrolls automatically to the top and the search box unfocuses basically on every letter being typed, which is super annoying.
Is there any easy way to exclude scrollTop or .animate from the addEventListener?
I want the user being able to type, have the content scrolled to the top and when they click anywhere on the page (scrolling down manually), the search box should unfocus.
You could make the search box only lose focus if the page is being scrolled down:
var content = document.querySelector('.content');
var currentPos = content.scrollTop;
content.addEventListener('scroll', function(e) {
if (content.scrollTop > currentPos) {
$('#search_box').blur();
}
currentPos = content.scrollTop;
});
You could refocus on the search box after the animation is complete:
$('#search_box').keyup(function() {
$('.content').animate({
scrollTop: 0
}, function() {
$('#search_box').focus();
});
}
The header on my site disappears when a user scrolls down the page. If the user begins to scroll up the header is displayed again, even if they're halfway down the page. If the user starts to scroll downwards, the header disappears again.
The effect works pretty well. Here's a CodePen showing the markup, css and javascript:
http://codepen.io/anon/pen/QpWXpj
From what I can see the only issue it has is if you scroll and and up really quickly. It's almost like the code can't react quick enough and the detached class is added when it's not needed. Which means in the demo you get the (lovely) red background even when you're at the top of the browser.
Can anyone help/suggest how the script could be amended to prevent this?
Thanks in advance!
You have to take out the
if (!currentScroll) {
header.removeClass('detached');
}
out of the else block so that the menu jump fix is applicable at all times and not only when the else condition is satisfied.
Check updated codepen: http://codepen.io/nashcheez/pen/KWKjjq
So your js code becomes:
/* ==========================================================================
#SHOW/HIDE HEADER
========================================================================== */
$(function() {
var previousScroll = 0, // previous scroll position
menuOffset = 70, // height of menu (once scroll passed it, menu is hidden)
detachPoint = 220, // point of detach (after scroll passed it, menu is fixed)
hideShowOffset = 5, // scrolling value after which triggers hide/show menu
header = $('.page-head');
$(window).scroll(function() {
if (header.hasClass('expanded')) return;
var currentScroll = $(this).scrollTop(),
scrollDifference = Math.abs(currentScroll - previousScroll);
// if scrolled past menu
if (currentScroll > menuOffset) {
// if scrolled past detach point add class to fix menu
if (currentScroll > detachPoint) {
var value = parseInt(header.css('transform').split(',')[5]);
console.log(value);
if (!header.hasClass('transitioning') && !header.hasClass('detached') && value != -110) {
header.addClass('transitioning').one('transitionend', function() {
console.log('transitionend');
header.removeClass('transitioning');
if (currentScroll > detachPoint) header.addClass('detached');
});
} else if (!header.hasClass('transitioning') && !header.hasClass('detached')) {
header.addClass('detached');
}
}
// if scrolling faster than hideShowOffset hide/show menu
if (scrollDifference >= hideShowOffset) {
if (currentScroll > previousScroll) {
// scrolling down; hide menu
if (!header.hasClass('invisible'))
header.addClass('invisible');
} else {
// scrolling up; show menu
if (header.hasClass('invisible'))
header.removeClass('invisible');
}
}
}
// only remove “detached” class if user is at the top of document (menu jump fix)
if (!currentScroll) {
header.removeClass('detached');
}
// If user is at the bottom of document show menu.
if ($(window).height() + currentScroll == $(document).height()) {
header.removeClass('invisible');
}
// Replace previous scroll position with new one.
previousScroll = currentScroll;
});
});
/* ==========================================================================
#SHOW/HIDE NAVIGATION
========================================================================== */
/*
* Creates classes to enable responsive navigation.
*/
// Wait for the DOM to be ready (all elements printed on page regardless if
// loaded or not).
$(function() {
// Bind a click event to anything with the class "toggle-nav".
$('.page-head__toggle').click(function() {
if ($('body').hasClass('show-nav')) {
$('body').removeClass('show-nav').addClass('hide-nav');
setTimeout(function() {
$('body').removeClass('hide-nav');
}, 500);
} else {
$('body').removeClass('hide-nav').addClass('show-nav');
}
// Deactivate the default behavior of going to the next page on click.
return false;
});
});
$(".clickable").each(function(idx, elem) { // register click for slides
elem = $(elem);
elem.click(function() {
scaleClicked(elem);
});
});
function scaleClicked(elem) { // slide clicked
var id = elem.attr("id"),
num = id.slice(-1),
postId = "post"+num,
posts = $(".post");
posts.each(function(idx, p) {
if($(p).attr("id") == postId) {
scaleUp(p);
}
else {
scaleDown(p);
}
});
}
function scaleUp(item) {
$(item).animate({height:1000},1000);
}
function scaleDown(item) {
$(item).animate({height:30},1000);
}
I need to Increase div height on click, like example 01 and at the same time, That Div Must scroll to Top of the window Like Example 02. Can You Help With This.
i tried few ways but when div increase the height, but it is scrolling to beyond the top level. but i need to stop div,when it scroll to top level of the window.
Instead of using an anchor to scroll like the example you gave you can scroll like this, for example to scroll to the bottom of a div:
$("#mydiv").animate({ scrollTop: $('#mydiv')[0].scrollHeight}, 1000);
To scroll to an item in the div you can find the position to scroll to in a number of ways, there's a good discussion here:
How do I scroll to an element within an overflowed Div?
Im trying to remove the scrollbars and replace them with up/down buttons.
Im nearly there, everything works, I just want to remove the 'up' button when you are scrolled right to the top of the container. right now Ive just hidden it as a default and an onscroll event on the container, and then just do this to make the scroll button appear:
function OnScrollDiv (div) {
$(".up").show();
}
But if you scroll back up, the up button stays, of course.
Now I dont understand why my alternative wont work! What I want to do is just check at the scroll event whether the scrollTop() value is 0, if it is, dont show it. I dont know javascript, but I would imagine it would look like:
function OnScrollDiv (div) {
var n = $("#prodcont").scrollTop();
if (n = 0) {
//nothing
}
else {
$(".up").show();
}
But alas, this does not work. At all. Any suggestions?!
You can make use of onscroll event provided by window. Ref
window.onscroll = scroll;
function scroll () {
console.log("scroll event detected! " + window.pageXOffset + " " + window.pageYOffset);
if(window.pageXOffset == 0 && window.pageYOffset == 0)
console.log('hide buttons');
}
ive a problem which is driving me crazy. im trying to explain...
i have a very long scrolling page with about 10 divs, one below the other (no space between).
at the bottom of the viewing port is a button with an id and a position: fixed. when i scroll up or down the button is fixed while the divs move up or down.
i want to have different id's on the button depending on which div layer is in the viewing port. that means if one divlayer fills over 50% of the available space the href of the button should change...
i tried the inview.js, but the problem is, that 2 divs at the same time have the inview class...
my current code:
$('#div4, #div5, #div6').bind('inview', function (event, visible) {
if (visible == true) {
$(this).addClass("inview");
} else {
$(this).removeClass("inview");
}
});
var $div4 = $('#div4');
if($div4.hasClass("inview")){
$('#button1').attr('id', 'button2');
}
you see, every div which is in the viewport the button gets a new id.
has anyone of you a solution?
thanks ted
You can try to remove the inview class before adding it.. Something like this:
var $divs = $('#div4,#div5,#div6';
$divs.bind('inview', function (event, visible) {
$divs.not(this).removeClass("inview");
if (visible == true) {
$(this).addClass("inview");
}
});
Another suggestion is to use the Waypoints plugin and fire when the div crosses the 50% mark.
The only difficult part is that depending on the direction you'll need to select the current div or the one above.
Plugin: http://imakewebthings.com/jquery-waypoints/
Demo: http://jsfiddle.net/lucuma/nFfSn/1/
Code:
$('.container div').waypoint(function (direction) {
if (direction=='up')
alert('hit ' + $.waypoints('above')[$.waypoints('above').length-2].id);
else
alert('hit ' + $(this).attr('id'));
}, {
offset: $.waypoints('viewportHeight') / 2
});