Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-01-2013, 02:15 PM   PM User | #1
m2244
Regular Coder

 
Join Date: Jun 2012
Posts: 129
Thanks: 1
Thanked 1 Time in 1 Post
m2244 is an unknown quantity at this point
Having a small problem with this scroll function

Hello Good People,

So, here's the thing; I have a glossary div on the page which scrolls to a selected term. It works well, until the user selects a second term. In other words the user selects an initial term, the glossary scrolls to that term, then the user selects a second term (while the glossary is still open and scrolled), now the glossary gets confused and scrolls to a point that makes no sense.

You can see in the code below, I tried to reset the scroll to the top (which is actually at 92) but it still gets confused. I am wondering if it does not have time to get back to the top before it tries to run the animated scroll.

Code:
	var thisTerm = $('#glossaryContent span').filter(function() {
		return $(this).text() == evt;
	})
	
	alert(thisTerm.html());
	var spanOffset = thisTerm.position().top;
	$('#glossaryContent').scrollTop(92);
	$('#glossaryContent').animate({scrollTop: spanOffset}, spanOffset/2);
m2244 is offline   Reply With Quote
Old 03-01-2013, 04:40 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,392
Thanks: 18
Thanked 351 Times in 350 Posts
sunfighter is on a distinguished road
I think I'd get the position we're at (if that's possible) or store in in a global or a session cookie or session storage. Then do math on it to get the amount of scroll.

If your at 144 and the next term is at 94 you would do move back up 50. (94 - 144).
sunfighter is offline   Reply With Quote
Old 03-01-2013, 08:21 PM   PM User | #3
m2244
Regular Coder

 
Join Date: Jun 2012
Posts: 129
Thanks: 1
Thanked 1 Time in 1 Post
m2244 is an unknown quantity at this point
Quote:
Originally Posted by sunfighter View Post
I think I'd get the position we're at (if that's possible) or store in in a global or a session cookie or session storage. Then do math on it to get the amount of scroll.

If your at 144 and the next term is at 94 you would do move back up 50. (94 - 144).
Thanks for the help. I will try this right now.
m2244 is offline   Reply With Quote
Old 03-01-2013, 09:12 PM   PM User | #4
m2244
Regular Coder

 
Join Date: Jun 2012
Posts: 129
Thanks: 1
Thanked 1 Time in 1 Post
m2244 is an unknown quantity at this point
Well, it sounded easy enough. Any suggestions?

Code:
var prevTermPosition = 0;
function glossScroll(evt)
{
	//alert(evt);
	//*********** This gets the offset of the target span tag. Offset is the offset distance from the parent element. ******************/
	//var thisTerm = $('#glossaryContent').find('span:contains(' + evt + ')');
	
	var thisTerm = $('#glossaryContent span').filter(function() {
		return $(this).text() == evt;
	})
	
	var scrollPosition = thisTerm.position().top;
	var currentTermPosition = thisTerm.position().top;
	var difference = currentTermPosition - prevTermPosition;
	//alert(currentTermPosition + " - " + scrollPosition + " = " + difference);
	alert(difference);
	//$('#glossaryContent').scrollTop(92);
	$('#glossaryContent').animate({scrollTop: prevTermPosition + difference}, currentTermPosition/2);
	
	prevTermPosition = currentTermPosition;
	
}
m2244 is offline   Reply With Quote
Old 03-02-2013, 12:50 AM   PM User | #5
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
did you try what I told you before, about scrolling back to the top of the div before scrolling down again?

Code:
function glossScroll(evt)
{
	//alert(evt);
	//*********** This gets the offset of the target span tag. Offset is the offset distance from the parent element. ******************/
	//var thisTerm = $('#glossaryContent').find('span:contains(' + evt + ')');
	
	var thisTerm = $('#glossaryContent span').filter(function() {
		return $(this).text() == evt;
	})
	
	var scrollPosition = thisTerm.position().top;
$('#glossaryContent').scrollTop(0);
$('#glossaryContent').scrollTop(scrollPosition);

}
xelawho is offline   Reply With Quote
Old 03-02-2013, 12:17 PM   PM User | #6
m2244
Regular Coder

 
Join Date: Jun 2012
Posts: 129
Thanks: 1
Thanked 1 Time in 1 Post
m2244 is an unknown quantity at this point
Quote:
Originally Posted by xelawho View Post
did you try what I told you before, about scrolling back to the top of the div before scrolling down again?

Code:
function glossScroll(evt)
{
	//alert(evt);
	//*********** This gets the offset of the target span tag. Offset is the offset distance from the parent element. ******************/
	//var thisTerm = $('#glossaryContent').find('span:contains(' + evt + ')');
	
	var thisTerm = $('#glossaryContent span').filter(function() {
		return $(this).text() == evt;
	})
	
	var scrollPosition = thisTerm.position().top;
$('#glossaryContent').scrollTop(0);
$('#glossaryContent').scrollTop(scrollPosition);

}
Yes I did try that but I think that the scroll back to the top was still in progress when the animated scroll down began which caused it to scroll to a random position.
m2244 is offline   Reply With Quote
Old 03-02-2013, 01:59 PM   PM User | #7
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
hmm. unlikely. scrollTop happens pretty quickly. have you tried using offset().top and getting the value after scrolling back up?

Code:
$('#glossaryContent').scrollTop(0);
var scrollPosition = thisTerm.offset().top;
$('#glossaryContent').scrollTop(scrollPosition);
xelawho is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:08 PM.


Advertisement
Log in to turn off these ads.