Slide in from the left - javascript

This is basically my current website:
https://jsfiddle.net/s0wgnvk2/
And now I have the problem in the #About section when I click right it goes to #right section and, you can't see it in fiddle but in my webpage the transition is really smooth and I like it how it is, but I just don't know how to make it work for the left side since it is positioned left:-100%.
$(function() {
$('#About a').bind('click',function(event){
var $anchor = $(this);
$('html, body').stop().animate({
scrollLeft: $($anchor.attr('href')).offset().left
}, 1000);
event.preventDefault();
});
});
I know about the fullPage.js plugin but I'd like to have my own solution and web layout even thought fullPage is probably a lot better.
I would appreciate if you could help me out
EDIT
Fiddle corrected: https://jsfiddle.net/s0wgnvk2/1/

scrollLeft denotes the scroll bar position from the left, see ref here. So it cannot be negative (the left most position is 0).
What you need might be marginLeft, try:
$('html, body').animate({ marginLeft: '100%' });
Here is a working jsfiddle example. Note that I have make the header position: fix, so that it knows where to anchor and how to width itself when the left margin of the element changes.

Use animation not scroll
see what issue you face when you use scroll https://jsfiddle.net/kevalbhatt18/s0wgnvk2/4/
And as you said in your site animation is working smoothly and not in fiddle
that is because you haven't selected jauery from droupdown which is
present in left side of fiddle editor so your animation is not working so if you see your console it will give you Uncaught ReferenceError: $ is not defined in your example https://jsfiddle.net/s0wgnvk2/
Left right animation Example: https://jsfiddle.net/kevalbhatt18/vunL9ec2/
$('#About a').bind('click', function (event) {
var $anchor = $(this);
if ($anchor.context.innerHTML === "RIGHT" ) {
$($anchor.attr('href')).stop().animate({
left: '-=100%'
}, 1000);
} else if ($anchor.context.innerHTML === "LEFT") {
$($anchor.attr('href')).stop().animate({
left: '+=100%'
}, 1000);
} else {
console.log($(this).context.offsetParent.id )
$('#'+$(this).context.offsetParent.id ).stop().animate({
left: $(this).context.offsetParent.id ==='right'?'+=100%':'-=100%'
}, 1000);
}

Related

click on a link to scroll to a particular div (jquery)

When i click a link and try to scroll to a particular div with slow animation the whole page get scrolled instead of that particular div.
I am sharing the link to the plnkr below open it in full screen mode.
http://plnkr.co/edit/5ZlY7ivJ2xwckVeyIRzO?p=preview
for full screen loading
http://run.plnkr.co/HV426GUKePHeJPfS/
The problem is that when the content present in the right hand side panel is clicked (only recommendation and cme & Attended is to cliked) the middle-panel should be scrolled to show that particular div on the top.
Instead what's happening is the whole page is getting scrolled thus making the UI not much of use.
I have tried using the following two javascript code for showing some animation and scrolling the middle_profile div or mm div but none of them is working.
for scrolling middle_profile div
$("#bb").click(function() {
$('.middle_profile').animate({
scrollTop: $("#recommendationDiv").position().top
}, 'slow');
});
$("#bb1").click(function() {
$('.middle_profile').animate({
scrollTop: $("#CMEDiv").offset().top
}, 'slow');
});
for scrolling mm div
var scrolled = 0;
$("#bb").on("click", function() {
scrolled = scrolled - 300;
$(".mm").animate({
scrollTop: scrolled
}, 50);
});
Is there another way of doing it through jquery or some other library is to be included for smooth scrolling the page?
You just need to put inside the $(document).ready() your code, and set in your css the position: relative; top:0; to your div who will be focus.
$(document).ready(function(){
$("#bb").click(function() {
$('html,body').animate({
scrollTop: $("#recommendationDiv").offset().top
}, 'slow');
});
$("#bb1").click(function() {
$('html,body').animate({
scrollTop: $("#CMEDiv").offset().top
}, 'slow');
});
// var scrolled = 0;
// $("#bb").on("click" ,function(){
// scrolled=scrolled-300;
// $(".mm").animate({
// scrollTop: scrolled
// },50);
// });
})

Scroll to specific div from another page minus X distance

I'm using the following script to perform the action described in my title.
<script type="text/javascript">
var jump=function(e)
{
if (e){
e.preventDefault();
var target = $(this).attr("href");
}else{
var target = location.hash;
}
$('html,body').animate(
{
scrollTop: $(target).offset().top
},1000,function()
{
location.hash = target;
});
}
$('html, body').hide();
$(document).ready(function()
{
$('a[href^=#]').bind("click", jump);
if (location.hash){
setTimeout(function(){
$('html, body').scrollTop(0).show();
jump();
}, 0);
}else{
$('html, body').show();
}
});
</script>
It works perfectly fine. However I have a fixed header which covers up the div once it's scrolled to. I'd like to minus 80px from the desired scroll target. How can I modify the code to do this?
Here's a live version.
robertkoh.net/Electrotemp/index.html
Click on 'ducted vacuum systems'. It takes you to the products and services page and scrolls to the appropriate section. As you can see the fixed header covers part of it.
Solution 1: Subtract 80 pixels and do not re-set location.hash
You only have to change this block:
$('html,body').animate({
scrollTop: $(target).offset().top
}, 1000, function() {
location.hash = target;
});
Into the following code:
$('html,body').animate({
scrollTop: $(target).offset().top - 80
}, 1000);
The added - 80 will subtract those 80 pixels, so the scrolling will stop earlier.
Removing location.hash = target; (that was called after the scroll animation was finished) fixes the problem of jumping back to the old position. This code was re-setting the hash tag which caused the browser to scroll back again. But be aware that clicking site internal hash links would not update the hash in the URL bar any more.
Solution 2: Move your page contents into a separate scrollable <div>
Create a new <div id="container"> that starts after <!--end slideMenu--> and ends before </body>.
Change the line $('html,body').animate({ into $('#container').animate({.
Remove margin-top: 70px; from .titlebar.
Add this CSS for the #container element:
#container {
position: fixed;
top: 70px;
bottom: 0;
left: 0;
overflow: auto; /* enable scrolling */
}
Doing it this way has some advantages:
You do not have to add - 80.
You do not have to remove location.hash = target;.
A browser without JavaScript enabled jumps to the right position.

How do I make a scroll to top button appear when a pre-determined part of the page appears?

I've got a scroll to top button I implemented via code that I got from one of the numerous examples out there. It works great. I changed the timing function for its appearance so that instead of appearing after 100 pixels it appears after 1340. I did this because there's some boxes on the right and I don't want the button to appear until they've been scrolled by and empty space thus appears for the button to fade in. The timing looked perfect -- the button appeared after what I figured was 1340 pixels from the top of the page.
However, I noticed that on a different (larger) screen the button didn't appear at the right time, but later. What I realized (if I gather correctly) was that the 1340 specification wasn't an absolute number of pixels from the top of the page, but was how many pixels had been scrolled through from the bottom of the browser window. For example, if I re-size the height of my browser window from, say, 1000 pixels to 400 pixels and reload the page, the button will appear 600 pixels too soon.
So my question is, is there a way to change my code so that the button appears only once a certain part of the page, measured from the top of the window, appears on screen?
Here's the javascript I'm currently using:
<script type="text/javascript">
$(document).ready(function () {
$(window).scroll(function () {
if ($(this).scrollTop() > 1340) {
$('#scrollup').fadeIn();
} else {
$('#scrollup').fadeOut();
}
});
$('#scrollup').click(function () {
$("html, body").animate({ scrollTop: 0 }, 500);
return false;
});
});
</script>
Thanks for any help.
Try this one--
<script type="text/javascript">
$(document).ready(function () {
ToggleScrollUp();
$(window).scroll(function () {
ToggleScrollUp();
});
$('#scrollup').click(function () {
$("html, body").animate({ scrollTop: 0 }, 500);
return false;
});
});
function ToggleScrollUp() {
if ($(".yourbox").offset().top < $(window).scrollTop() + $(window).height()) {
$('#scrollup').fadeIn();
} else {
$('#scrollup').fadeOut();
}
}
</script>
Check out this fiddle: http://jsfiddle.net/ro39dkaL/2/
Instead of using 1340 calculate the position of the bottom of the boxes instead, you can calculate the position on the fly so it's always accurate to the situation:
$(document).ready(function () {
var elem = $('#weblinks');
var bottom = $(elem).position().top+$(elem).outerHeight(true)
$(window).scroll(function () {
if ($(this).scrollTop() > bottom) {
$('#scrollup').fadeIn();
} else {
$('#scrollup').fadeOut();
}
});
$('#scrollup').click(function () {
$("html, body").animate({ scrollTop: 0 }, 500);
return false;
});
});
the 1340 specified is hard coded which wont be accurate in all cases.
A better solution is to find the height of the element w.r.t the browser window.
the jquery .offset() can be used.
var offset = $("#child").offset();
var fromTop = offset.top - $(window).scrollTop();
I made this fiddle and I use another way: as you can see, I placed a div with id scroll_toggle. When the user reaches this div (obviously you can remove the text, this is just a demonstration).
This is the fiddle: http://jsfiddle.net/afilini/7633pr0r/

scrollTop suddenly not working for sticky menu

I had this working with no problems for the entire build of the site. Then, the day I was supposed to launch, the sticky menu stopped working right. The menu is supposed to start at the bottom, scroll to the top, then stick (position: fixed).
Now, it scrolls about 10px and then jumps to the top. Why is the scrollTop distance not calculating correctly?
Live site at [site no longer exists]
Here's the code for the sticky menu. I'm also using JS to set min-height of divs to window height, but haven't included that code here.
$(function(){
var stickyRibbonTop = $('#wrapper-wcf53badf7ebadf7').offset().top;
$(window).scroll(function(){
if( $(window).scrollTop() > stickyRibbonTop ) {
$('#wrapper-wcf53badf7ebadf7').css({position: 'fixed', top: '0px', 'background-image':'url(http://amarshall.360zen.com/wp-content/uploads/2014/07/menu-fade-background2.png)'});
$('#block-bcf53bf14093931c').css({display: 'block'});
} else {
$('#wrapper-wcf53badf7ebadf7').css({position: 'static', top: '0px','background-image':'none'});
$('#block-bcf53bf14093931c').css({display: 'none'});
}
});
});
Thanks in advance for any help! I'm not a JS or jQuery expert yet, so any suggestions for cleaning things up would be appreciated.
NOTE: The site is built on WordPress, so no-conflict mode is in effect.
I think you are initialising the sticky menu function before you set the min-height of $('big-div').
On page load, the menu starts at 54px from the top, and so when you store the offset().top value as stickyRibbonTop, it is stored at 54px. Then on your scroll event you are comparing against this.
Try setting the min-height of the divs first in your code, then run this same script afterwards. The value of stickyRibbonTop should then be correct.
Bear in mind that you will need to reset stickyRibbonTop every time the window.height() is updated, so you should probably make this sticky menu function a named function and call it at the end of the wrapper_height function. something like this:
function stickyNav() {
var stickyRibbonTop = $('#wrapper-wcf53badf7ebadf7').offset().top;
$(window).unbind('scroll', scrollEvent);
$(window).on('scroll', stickyRibbonTop, scrollEvent);
};
function scrollEvent(event) {
var stickyRibbonTop = event.data;
if ($(window).scrollTop() > stickyRibbonTop) {
$('#wrapper-wcf53badf7ebadf7').css({ position: 'fixed', top: '0px', 'background-image': 'url(http://www.adammarshalltherapy.com/wp-content/uploads/2014/07/menu-fade-background2.png)' });
$('#block-bcf53bf14093931c').css({ display: 'block' });
}
else {
$('#wrapper-wcf53badf7ebadf7').css({ position: 'static', top: '0px', 'background-image': 'none' });
$('#block-bcf53bf14093931c').css({ display: 'none' });
}
};
function wrapper_height() {
var height = $(window).height();
var wrapperheight = height - 75;
wrapperheight = parseInt(wrapperheight) + 'px';
$(".bigDiv").css('min-height', wrapperheight);
$("#wrapper-wcf53bad125d7d9a").css('height', wrapperheight);
stickyNav();
}
$(function () {
wrapper_height();
$(window).bind('resize', wrapper_height);
});

cant toggle slide the div left

I am having trouble sliding a div from the left side, I have done this before and it worked fine but i'm not sure this time for some reason it doesnt really slide at all.
it seems to slide a couple milimeters then appears to .show the rest of the div.
I have tried
$('#menu_area').toggle('slide', { direction: 'left' }, 1000);
and
var left = $('#menu_area').offset().left;
$("#menu_area").css({left:left}).animate({"left":"140px"}, 500, function(){
this is my link
http://2click4.com/new/place2.php?id=CoQBcQAAAGqvOgbp0tJu7kVVn9hxur12lk85dSxYZiWj_2w8aL8yzahacGeo1h9ZZ0cAn2enEK7LirrOR8KBCzDhEdmpRbzlJt8000Ufvbct6lP4VUYQkSDXHq6YdFH_w799dw4HUcIz8pimNOdnIRS3hF8DoAt6RfZn7zC-cLgVvnSH7KdrEhDN4vYCBQkmmat2HkYPJ1S6GhRxB-UeiOXywY_f5qRgL19SVKUCag
DEMO http://jsfiddle.net/BywL4/
try to animate width if oyu want sliding effet as show in above link
$(document).on('click','button',function(){
if($("#expand").css('width') == '0px') {
$("#expand").animate({"width":"500px"}, "slow");
} else {
$("#expand").animate({"width":"0px"}, "slow");
}
});

Categories