Custom Scroller is Choppy. Suggestions for code approach?
Hi all.
I am making a scrollbar. It simply calculates the percentage it has moved within its width and then moves the larger object the same percentage of ITS width. Standard scroller logic I would presume.
I think the issue is WHEN I choose to run the function that moves the larger element. Could you look at the code I posted at the URL below and see if you would do anything different?
I will also post the snippet relative to this one function here.
Here is the page:
Here is the code snippet:
- $(window).load(function () {
-
- function rw_setPortfolioPos(scroll_button_new_posPassed){
-
- //width of full portfolio wrapper
- var port_wrapper_width = document.getElementById("portfolio_wrapper").offsetWidth;
-
- //width of scroll wrapper
- var scroll_wrapper_width = document.getElementById("scroller_wrapper").offsetWidth;
-
- //location of scroll wrapper (left edge default)
- var scroll_wrapper_pos = document.getElementById("scroller_wrapper").offsetLeft;
-
- //distance scroll button is from scroll wrapper left edge
- var scroll_button_distance = Math.abs((scroll_wrapper_pos - scroll_button_new_posPassed));
-
- //calc this distance with width of scroll wrapper to get perc moved
- var scroll_percentage = scroll_button_distance/scroll_wrapper_width;
- //now move portfolio wrapper the same perc
- var port_distance_to_move = (scroll_percentage*port_wrapper_width)*-1;
-
- $('#portfolio_wrapper').css({
- left: port_distance_to_move + 'px',
- });
-
-
- }
-
-
-
- //THIS GATHERS ALL OF THE INFORMATION FROM THE DIV
- $('#portfolio_scroller_button').click(function(){
- var myTarget = $(this);
- var myWidth = myTarget.width();
- var myHeight = myTarget.height();
- var myPosition = myTarget.position();
- var finalLeftPosition = myPosition.left;
- var finalTopPosition = myPosition.top;
- break;
- });
-
- $('#portfolio_scroller_button').mousedown(function() {
-
- $(document).bind('mousemove',function(e){
- var rw_mouseXPos = e.pageX;
-
- $('#portfolio_scroller_button_div').css({
- left: rw_mouseXPos + 'px',
- });
- });
-
- //location of scroller button when moved
- var scroll_button_new_pos = document.getElementById("portfolio_scroller_button_div").offsetLeft;
//THIS IS WHERE I AM EXECUTING THE FUNCTION
- rw_setPortfolioPos(scroll_button_new_pos);
-
- });
-
-
- $(document).mouseup(function() {
- $(document).unbind('mousemove');
- alert("unbound " + scroll_button_new_pos);
- });
- });//END WINDOW LOAD FUNCTION
I'm not sure if the number of calculations for MOUSEMOVE is causing the issue?
Perhaps I need to place the call to rw_setPortfolioPos() at a different point in the order of execution?
Maybe I should use a totally different approach for tracking the position of the scroll button?
Thanks for your help. I tried to clean the code as best as I could above.
Rich