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 04-18-2011, 02:52 PM   PM User | #1
paffley
New Coder

 
Join Date: Sep 2010
Posts: 86
Thanks: 24
Thanked 0 Times in 0 Posts
paffley is an unknown quantity at this point
Help with Jquery mouse wheel

Hi guys,

I have this piece of code and i cannot get it to work, Would someone please take a look and see if they can see anything wrong in the code, thanks in advance.

Code:
<script type="text/javascript">
$(document).ready(function()
{
   // get slider height
   var sliderHeight = $('#jQuerySlider2').height();

   // get total height of slider content
   var scrollHeight = $("#HOME-LAYER").outerHeight();

   // create content wrapper , required for scrolling 
   $('#HOME-LAYER').wrapInner('<div style="position:relative;float:left" class="layer-content-1" />');

   // make layer the same size as the slider
   //$('#HOME-LAYER').css("height", sliderHeight);

   // hide overflow
   $('#HOME-LAYER').css('overflow', 'hidden');

   // expand content
   $('.layer-content-1').css("height", scrollHeight);

   // set max value for slider
   $('#jQuerySlider2').slider("option", "min", -(scrollHeight - sliderHeight));
   $('#jQuerySlider2').slider("option", "max", 0);
   $('#jQuerySlider2').slider("option", "value", 0);

   // mousewheel settings
   var speed = 5;//set the speed of the scroll
   var sliderVal = $("#jQuerySlider2").slider("value");//read current value of the slider
		
   sliderVal += (delta*speed);//increment the current value
 
   $("#jQuerySlider2").slider("value", sliderVal);//and set the new value of the slider
		
   var topValue = -((100-sliderVal)*difference/100);//calculate the content top from the slider position
		
   if (topValue>0) topValue = 0;//stop the content scrolling down too much
   if (Math.abs(topValue)>difference) topValue = (-1)*difference;//stop the content scrolling up too much
		
   $("#HOME-LAYER").css({top:topValue});//move the content to the new position

   event.preventDefault();//stop any default behaviour

});</script>
*The slider works fine, I just cant get the mouse wheel effect to work with it, i have the jquery mousewheel js file but it wont scroll with the mouse.


Cheers,
paffley
paffley is offline   Reply With Quote
Old 04-18-2011, 06:06 PM   PM User | #2
SB65
Senior Coder

 
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,817
Thanks: 9
Thanked 681 Times in 675 Posts
SB65 will become famous soon enoughSB65 will become famous soon enough
That code looks oddly familiar....

You aren't calling the mousewheel function - I'm assuming you're using Brandon Aaron's plugin? Your code needs to be within a function like this:

Code:
$('#mydiv').mousewheel(function(event,delta){

...your code....

});

Last edited by SB65; 04-18-2011 at 06:28 PM..
SB65 is offline   Reply With Quote
Old 04-18-2011, 06:57 PM   PM User | #3
paffley
New Coder

 
Join Date: Sep 2010
Posts: 86
Thanks: 24
Thanked 0 Times in 0 Posts
paffley is an unknown quantity at this point
Hi SB65! thanks for the reply, SB as in simonbattersby.com? if it is then its nice to meet ya, Am learning jquery at the moment and learnt alot from your site, thank you! Yeh im trying to implement the mousescroll into what I had already from the jquery slider.

Ive added the function, i totally forgot about that part! i read it on the website too! doh!

I still cant seem to get it to work, the slider works but still no mouse scroll this is what I have below:

Code:
<script type="text/javascript">
$(document).ready(function()
{

  // get slider height
   var sliderHeight = $('#jQuerySlider2').height();

   // get total height of slider content
   var scrollHeight = $("#HOME-LAYER").outerHeight();

   // create content wrapper , required for scrolling 
   $('#HOME-LAYER').wrapInner('<div style="position:relative;float:left" class="layer-content-1" />');

   // make layer the same size as the slider
   //$('#HOME-LAYER').css("height", sliderHeight);

   // hide overflow
   $('#HOME-LAYER').css('overflow', 'hidden');

   // expand content
   $('.layer-content-1').css("height", scrollHeight);

   // set max value for slider
   $('#jQuerySlider2').slider("option", "min", -(scrollHeight - sliderHeight));
   $('#jQuerySlider2').slider("option", "max", 0);
   $('#jQuerySlider2').slider("option", "value", 0);

});

   // mousewheel settings
   $('#HOME-LAYER').mousewheel(function(event,delta){

   var speed = 5;//set the speed of the scroll
   var sliderVal = $("#jQuerySlider2").slider("value");//read current value of the slider
		
   sliderVal += (delta*speed);//increment the current value
 
   $("#jQuerySlider2").slider("value", sliderVal);//and set the new value of the slider
		
   var topValue = -((100-sliderVal)*difference/100);//calculate the content top from the slider position
		
   if (topValue>0) topValue = 0;//stop the content scrolling down too much
   if (Math.abs(topValue)>difference) topValue = (-1)*difference;//stop the content scrolling up too much
		
   $("#HOME-LAYER").css({top:topValue});//move the content to the new position

   event.preventDefault();//stop any default behaviour

});
</script>
I must have something named wrong but cant see anything, any help is hugely appreciated


Cheers,
paffley
paffley is offline   Reply With Quote
Old 04-18-2011, 07:16 PM   PM User | #4
SB65
Senior Coder

 
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,817
Thanks: 9
Thanked 681 Times in 675 Posts
SB65 will become famous soon enoughSB65 will become famous soon enough
Almost, but now you don't have the mousewheel function included within $(document).ready - so currently it never gets called.
SB65 is offline   Reply With Quote
Old 04-18-2011, 07:26 PM   PM User | #5
paffley
New Coder

 
Join Date: Sep 2010
Posts: 86
Thanks: 24
Thanked 0 Times in 0 Posts
paffley is an unknown quantity at this point
Quote:
Originally Posted by SB65 View Post
Almost, but now you don't have the mousewheel function included within $(document).ready - so currently it never gets called.
K thanks SB, do I add it into the $(document).ready line or can it be inserted on a seperate line?

Im proper stuck now :P



Cheers,
paffley
paffley is offline   Reply With Quote
Old 04-18-2011, 07:32 PM   PM User | #6
SB65
Senior Coder

 
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,817
Thanks: 9
Thanked 681 Times in 675 Posts
SB65 will become famous soon enoughSB65 will become famous soon enough
You just need it within the document.ready, so the document ready function includes both the slider code and the mousewheel code:

Code:
<script type="text/javascript">
$(document).ready(function()
{

  // get slider height
   var sliderHeight = $('#jQuerySlider2').height();

   // get total height of slider content
   var scrollHeight = $("#HOME-LAYER").outerHeight();

   // create content wrapper , required for scrolling 
   $('#HOME-LAYER').wrapInner('<div style="position:relative;float:left" class="layer-content-1" />');

   // make layer the same size as the slider
   //$('#HOME-LAYER').css("height", sliderHeight);

   // hide overflow
   $('#HOME-LAYER').css('overflow', 'hidden');

   // expand content
   $('.layer-content-1').css("height", scrollHeight);

   // set max value for slider
   $('#jQuerySlider2').slider("option", "min", -(scrollHeight - sliderHeight));
   $('#jQuerySlider2').slider("option", "max", 0);
   $('#jQuerySlider2').slider("option", "value", 0);

//});move the closing bracket for document-ready from here...

   // mousewheel settings
   $('#HOME-LAYER').mousewheel(function(event,delta){

   var speed = 5;//set the speed of the scroll
   var sliderVal = $("#jQuerySlider2").slider("value");//read current value of the slider
		
   sliderVal += (delta*speed);//increment the current value
 
   $("#jQuerySlider2").slider("value", sliderVal);//and set the new value of the slider
		
   var topValue = -((100-sliderVal)*difference/100);//calculate the content top from the slider position
		
   if (topValue>0) topValue = 0;//stop the content scrolling down too much
   if (Math.abs(topValue)>difference) topValue = (-1)*difference;//stop the content scrolling up too much
		
   $("#HOME-LAYER").css({top:topValue});//move the content to the new position

   event.preventDefault();//stop any default behaviour

});

})//to here...
</script>
SB65 is offline   Reply With Quote
Old 04-19-2011, 09:03 AM   PM User | #7
paffley
New Coder

 
Join Date: Sep 2010
Posts: 86
Thanks: 24
Thanked 0 Times in 0 Posts
paffley is an unknown quantity at this point
hi SB, im still having problems with it, not sure what it is as ive set everything out the same etc.....




cheers,
paffley
paffley is offline   Reply With Quote
Old 04-19-2011, 05:33 PM   PM User | #8
SB65
Senior Coder

 
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,817
Thanks: 9
Thanked 681 Times in 675 Posts
SB65 will become famous soon enoughSB65 will become famous soon enough
Can you give a link to your page?
SB65 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 12:49 PM.


Advertisement
Log in to turn off these ads.