I would like to change a ul style on scrolling and arrive to div using jQuery, explanation down.
CSS:
#menu {
background-color:#ccc;
position:fixed;
width:100%;
}
.menutext {
padding:25 40 30 !important;
display:inline-block;
}
.menutext2 {
padding:25 40 0 !important;
display:inline-block;
color:red;
}
.alldivs {
width:300px;
height:200px;
background-color:a9a9a9;
}
HTML code:
<div id="menu">
<div class="menutext">Change the style of me to .mebutext2 on arriving to DIV1</div>
<div class="menutext">Change the style of me to .mebutext2 on arriving to DIV2</div>
<div class="menutext">Change the style of me to .mebutext2 on arriving to DIV3</div>
</div>
<br><br><br>
<div class="alldivs"><div id="DIV1">DIV1</div></div>
<br><br><br><br>
<div class="alldivs"><div id="DIV2">DIV2</div></div>
<br><br><br><br>
<div class="alldivs"><div id="DIV3">DIV3</div></div>
<br><br><br><br>
I want to change the div with the class "menutext" to class "menutext2" on scrolling and arriving to the top of the divs(first ul's class change from menutext1 to menutext2 on arriving to the div with the id DIV1, second ul's class change from menytext1 to menutext2 on arriving to the div with the id DIV2 AND the first ul's class return to menutext1 AS IT WAS and so on.
Yes, you need jQuery, but I do not understand: You must apply menutext2 class when the scroll bars to see div1 or when you click div1?
set event (click or scroll and apply)
It seems you want same as what i implemented on my page.
Have look on menu section on http://s-yadav.github.com/contextMenu.html
As per your requirement try this.
HTML
JS
Update:
Generic method.
The second method is shorter and generic but less efficient than first one as every time loop will go inside scroll event. and scroll event are executed very frequently.
If the number of '.contentDiv' is less, i will prefer to follow first approach else follow second approach.
Yup, done it with jsFiddle try this
Replace .css with .addClass and .removeClass. I use parent div of DIV1-3 because you was style a height on it parent.
});
This should do what you're asking, using only jQuery:
Note that to make it work I had to apply the
alldivs
class on yourdiv1
,div2
,div3
, etc. and give the menu items classes corresponding to their IDs.Demo in this jsFiddle: http://jsfiddle.net/ss7YF/
EDIT If you want only the closest one to be highlighted (for a fixed menu I'm guessing?) use this:
jsFiddle: http://jsfiddle.net/ss7YF/1/
Try something like this jsFiddle
});
I have this set to add
.menutext2
after you've scrolled down 275px, approximate top of div2, it shouldn't be hard to figure it out from here.You can use YAHOOs YUI framework to write a javascript along the lines of something like this: