Links changing when hovering Main menu buttons? - php

So, I have a main menu bar on my website and a "submenu" bar just underneath. The submenu bar is currently not linked at all to the main menu bar (I mean by this, on any page that you are, the submenu bar is the same).
This is what it looks like: (sorry for the ugly paint skills)
Anyway, I'm trying to make the links in the submenu bar change when you hover one of the big buttons of the main menu.
This is what I tried in my html:
<div id = 'nav'>
<div id = 'hovermenu1'>
<div class = 'button-accueil'></div>
<div id = 'menu1'>
Link 1
Link 2
Link 3
</div>
</div>
<div id = 'hovermenu2'>
<div class = 'button-guide'></div>
<div id = 'menu2'>
Link 1
Link 2
Link 3
</div>
</div>
[...] <!-- And we go on like this until hovermenu6 -->
<div id = 'hovermenu6'>
<div class = 'button-contact'></div>
<div id = 'menu6'>
Link 1
Link 2
Link 3
</div>
</div>
</div>
And in my css:
#menu1 {
background:url(/images/menu.jpg) no-repeat left top;
text-align:center;
display:none;
float:left;
margin-left:16px;
margin-top:2px;
padding-top:9px;
width:700px;
height:33px;
}
#menu2 {
background:url(/images/menu.jpg) no-repeat left top;
text-align:center;
display:none;
float:left;
margin-left:16px;
margin-top:2px;
padding-top:9px;
width:700px;
height:33px;
}
[...] /* And we go on like this until menu6 */
#menu6 {
background:url(/images/menu.jpg) no-repeat left top;
text-align:center;
display:none;
float:left;
margin-left:16px;
margin-top:2px;
padding-top:9px;
width:700px;
height:33px;
}
#hovermenu1:hover #menu1 {
display: inline;
}
#hovermenu2:hover #menu2 {
display: inline;
}
[...] /* And we go on like this until hovermenu6 */
#hovermenu6:hover #menu6 {
display: inline;
}
This, unfortunately, doesn't work.
I figured out it kinda works when I didn't close all the div id="hovermenu1/2/3/4/5/6 and closed them all at the bottom but it still doesn't work properly at all. When you hover The 6th button, it considers that you are hovering all buttons and it tries to display all menus.
Could you help me figure out what I am doing wrong and how I could fix this please?
Thank you so so much if anyone can help me. I'm stuck on this thing for a while!
Regards,
Orangow.

I would simply your HTML and not try to tie the main buttons to the submenu quite as much. It's not necessary. You can simply make one div for each submenu, and hide all of them except the one that's active. Then use JavaScript to capture the hover event for each main menu item, and hide/show the submenu as necessary. For instance:
<div id="main1">Button</div><div id="main2">Button2</div><div id="main3">Button3</div>
<div id="sub1" style="display:none;">Link 1Link 2Link 3</div>
<div id="sub2" style="display:none;">Link 1Link 2Link 3</div>
<div id="sub3" style="display:none;">Link 1Link 2Link 3</div>
<script>
$(document).ready(function(){
$('#main1').hover(function(){ $('#sub1').show(); $('#sub2').hide(); $('#sub3').hide(); },
function(){ $('#sub1').hide(); }
$('#main2').hover(function(){ $('#sub2').show(); $('#sub1').hide(); $('#sub3').hide(); },
function(){ $('#sub2').hide(); }
$('#main3').hover(function(){ $('#sub3').show(); $('#sub2').hide(); $('#sub1').hide(); },
function(){ $('#sub3').hide(); }
});
</script>
This is kind of down-and-dirty, but hopefully, it makes sense and moves you along.

Related

Jquery display Div li child items onclick

In jquery, I need to dynamically display first two values and after while click remaining values should be display?
<div class="category_items">
<div class="category_item">
<div class="test">test1_1</div>
<div class="test">test1_2</div>
<div class="test">test1_3</div>
<div class="test">test1_4</div>
<div class="test">test1_5</div>
<div class="test">test1_6</div>
</div>
<button>Show / Hide</button>
<script type="text/javascript" src="//code.jquery.com/jquery-latest.js"></script>
<script>
var elems = $('.category_item', '.category_items').filter(function() {
return $(this).children().length > 2;
}).hide();
$('button').on('click', function() {
elems.toggle();
});
</script>
I want to display first two items by default when i click the button i want show full list
Do this with css with
HTML
<style>
li div{display:none;}
li:hover div {display:block;position:absolute;left:200px;width:100px;height:100px;background-color:red;}
</style>
<ul>
<li>123<div>1</div></li>
<li>222<div>2</div></li>
<li>333<div>3</div></li>
</ul>
enter code here
:gt selector will be good for this situation. I hope, this code helps you.
<script>
$('.category_item .test:gt(1)').hide();
$('button').on('click', function() {
$('.category_item .test:gt(1)').toggle();
});
</script>
If you want to change the number of elements which you want to show, just change number in gt() selector.
See this also but I not sure if is normal to use span as child of ul not li but you can put in last li another ul and when show this new ul.
This all li is show when zou hover ul bu zou can do to show all li onli when hove some think one as example + hover last show li(user no need to click to see al!).
<style>
ul span{display:none;}
ul:hover span {display:block;/*position:absolute;left:200px;background-color:red;*/}
</style>
<ul>
<li>123<div>1</div></li>
<li>22<div>2</div></li>
<span>
<li>223<div>3</div></li>
<li>223<div>3</div></li>
<li>223<div>3</div></li>
</span>
</ul>

How to create drop down menus on dynamically generated div fields

I am loading divs with information from the database using php code:
for ($i=0; $i < $stmt->rowCount(); $i++){
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$name = $row['uc_name'];
$image_url = $row['uc_image'];
$color = $row['uc_color'];
The creates a box for however many rows there are: http://jsfiddle.net/pKR5t/1/
I am trying to create a drop down menu for each item retrieved from the database. When the user hovers over class="header", the drop down menu should show but the list inside should be pertaining to that of the box hovered:
I believe I can use the .on() function to accomplish this but I am completely lost on how to use it. Any help on getting this accomplished would be helpful. Thanks
To use on and hover you will want to use the mouseenter and mouseleave events like so:
$("div.box div.header").on("mouseenter", function() { //mouseenter event
var currentBox = $(this).closest("div.box");
var currentTitleText = $(this).find("div.title").html();
//popup the list
}).on("mouseleave", function() { //mouseleave event
//close the list
});
For some additional info, check this post.
EDIT
Updated mouseover and mouseout to the slightly better mouseenter and mouseleave events.
You can do this without javascript using css.
If you insert your hover box inside your header div you can use css :hover to hide and show it on hover.
HTML
<div class="header">
<div class="title">PHP Row 1</div>
<div class="hoverBox">
<div class="hoverBoxTitle">Hover</div>
<div class="hoverBoxContent">Hover Content</div>
</div>
</div>
CSS
.hoverBox {
display: none;
position: absolute;
top: 0px;
left: 170px;
width:170px;
height:90px;
background:#333333;
z-index: 100;
}
.header:hover .hoverBox {
display: block;
}
Demo

My jQuery IMG center script fails in Chrome and Safari, but works IE?

I have an e-commerce site where I'm displaying all my products and the thumbnails need to be centered in a div. The centering works in IE(even in compatibility mode), FF, and Opera, but fails in Chrome and Safari. In Chrome and Safari the image stays on the top of the div and does not center. I've changed my CSS around trying to locate the problem, but I can't seem to find what's causing the issue? Anyone see anything?
IE (GOOD)
CHROME (BAD)
JQUERY
var _h = $('div.product-image').height();
$('div.product-image img').each(function()
{
var _top = (_h - $(this).height()) / 2;
$(this).css('margin-top',_top);
});
CSS
.product
{
float:left;
margin:5px;
width:200px;
height:200px;
border:1px solid #999;
}
.product-image
{
margin:2px auto;
width:194px;
height:145px;
text-align:center;
border:1px solid #999;
}
.product-image img
{
max-height: 100%;
max-width: 100%;
border:1pc solid #999;
}
HTML
<div id="content">
<a href="#">
<div class="product">
<div class="product-image">
<img src="1.jpg" />
</div>
<div class="product-model">sadf</div>
<div class="product-price"> : 234</div>
</div>
</a>
<a href="#">
<div class="product">
<div class="product-image">
<img src="2.jpg" />
</div>
<div class="product-model">sdaf</div>
<div class="product-bottom"> : 2345</div>
</div>
</a>
</div>
Here's the Fiddle link : http://jsfiddle.net/anaZD/
You might check this, it worked for me:
How to vertical align image inside div
div {position:relative;}
img {position:absolute;top:0;bottom:0;margin:auto;}
.image {min-height:50px}
div is your .product-image
img is .product-image img
Perhaps the problem is with margintop. jQuery CSS uses 'marginTop' rather than 'margin-top' and you might want those images instead to be positioned relatively based on your calculations on the original script.
Here is an example, can you clarify if that's what you are trying to accomplish? See attached the JsFiddle
The Javacript
var a=$;
a.noConflict();
var CountP=new Array();
jQuery(function(a){
a("div.product-image img").each(function(b,c){
CountP[c]=(( a(this).parent('div').parent('div').height() - a(this).height() ) / 2) +'px';
//Alert as we go for testing
alert('The difference that we are looking for this product is: ' + CountP[c] + ' and the height of this product is:' + a(this).height() );
To ensure browser compatibility try with marginTop for CSS or like in this example,
position:relative; top and left values;
a(this).css({'border':'2px inset red','position':'relative',
'top': CountP[c], 'left':'0.1em','margin':'auto','marginTop':'0.1px', 'display':'inline-block'});
})
});
Below a test on jsfiddle.net.. hope it help you out!
http://jsfiddle.net/mt5fk/93/
With your provided images ;) The JS is the same but it does not allow me to update with the link
var a=$;
a.noConflict();
var CountP=new Array();
jQuery(function(a){
a("div.product-image img").each(function(b,c){
CountP[c]=(( a(this).parent('div').parent('div').height() - a(this).height() ) / 2) +'px';
alert('The difference that we are looking for this product is: ' + CountP[c] + ' and the height of this product is:' + a(this).height() );
//to ensure browser compatibility try with marginTop for CSS or like in this example, position:relative; top and left values;
a(this).css({'border':'2px inset red','position':'relative','top': CountP[c], 'left':'0.1em','margin':'auto','marginTop':'0.1px', 'display':'inline-block'});
});
});
New JSFiddle
http://jsfiddle.net/mt5fk/94/

PHP: Fill block of an image with color and save the information

I'm looking to setup a page which holds an image of grid paper. These grids can be selected and depending on what type of link the user chooses the color will be filled and saved. Once all of the blocks are filled this will be archived and then a new sheet will be displayed. I wanted to use PHP / MySQL on the backend but was wondering what would be good client side?
Why a image for the grid? Better solution is to use a table or a list filled with divs to create the grid. Then use jQuery to fill in the backgrounds when the user clicks a link.
Example for the grid:
<ul id="grid">
<li>
<div class="cell" id="cell_id"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</li>
<li>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</li>
...
</ul>
CSS example:
<style>
#grid {
list-style: none;
}
#grid li {
height: 50px;
}
#grid .cell {
float: left;
width: 100px;
height: 50px;
border: 1px solid black;
}
</style>
And finally use jQuery to fill the cells when a user clicks a link. You can find the cell by calculating the rows and cells or just give the cells a id. The saving part can be done by a AJAX call to a page that will save the information to the DB.
Simple jQuery example (you have to think up the rest yourself):
<script>
$('link').click(function() {
//place check for new sheet here
var id = $(this).attr('id');
//set the bg color
$('cell_id').css('background-color', 'red'); //or color code #FF0000
//save the info
$.post('save_info.php', { cell_id: id, color: "red" }, function(data) {
alert('saved!');
});
});
</script>
You can find more info and download jQuery at: http://jquery.com/
The jQuery code to create a new sheet when all the cells are filled can be done with a little check in the click function. This check must count all the cells that have a bg color, is this the same as the total cells? Then create a new sheet by removing all the cells their bg color and by setting new id's. But you can figure this out by yourself ;).

jQuery rating system

I'm trying to make a rating system from 0, so far I have two classes on and off, there are total of 5 stars,
It would show the rating of the item, until the person hovers over them, then it would hilight the previous stars and hide the current rating,
<div id="stars">
<div class="on"></div>
<div class="on"></div>
<div class="on"></div>
<div class="off"></div>
<div class="off"></div>
</div>
the JS I got so far
$('#stars').mouseover(function() {
$(this).children().prevAll().andSelf().attr('class', 'on');
$(this).nextAll().attr('class', 'on');
});
Which only hilights all the starts and messes up everything, anyone could help out? Working too many hours and just can't get it to work..
Thanks!
If you're using this as a preview before a click, then you may want to use the hover function like this:
$('#stars .star').hover(
function() {
$(this).prevAll().andSelf().addClass('on');
$(this).nextAll().removeClass('on');
},
function() {
$(this).parent().children().removeClass('on');
}
);
That goes with the HTML in this jsFiddle where you can see it work: http://jsfiddle.net/jfriend00/aRTzQ/
I'd also suggest using addClass() and removeClass() rather than manipulating the attribute directly as it gives you more flexibility to have other classes on the object and it's more readable.
To make a click sticky so it then remembers that setting, you could use something like this: http://jsfiddle.net/jfriend00/QxADg/. The corresponding CSS/HTML is in the fiddle.
$('#stars .star').hover(
function() {
$(this).prevAll().andSelf().addClass('preview');
$(this).nextAll().removeClass('preview');
},
function() {
$(this).parent().children().removeClass('preview');
}
);
$('#stars .star').click(function() {
$(this).parent().children().removeClass('on');
$(this).prevAll().andSelf().addClass('on');
});
$('#stars').hover(
function() {$(this).addClass("inPreview");},
function() {$(this).removeClass("inPreview");}
);
There's a lot of plugins out there, such as the one PPrice links to. I agree with others that that would be the better way to go unless they don't meet your needs for some reason.
If you want to make this code work or if you're just doing this for the learning, maybe this is what you're trying to do?
You can hover over the "stars" (boxes) to see the colour change.
http://jsfiddle.net/pW3Sn/
$('#stars > div').mouseover(function() {
$(this).prevAll().andSelf().attr('class', 'on');
$(this).nextAll().attr('class', 'off');
});
Actually you can implement it using pure CSS.
HTML code:
<div id="stars">
<div class="star">
<div class="star">
<div class="star">
<div class="star">
<div class="star"></div>
</div>
</div>
</div>
</div>
</div>
And CSS:
#stars {
height: 16px; /* height of stars block */
}
#stars .star {
height: 16px; /* height of one star */
padding-left: 16px; /* width of one star */
background-image: url('http://url.to.your.star.image');
float: left;
}
#stars .star:hover,
#stars .star :hover {
background-position: 0px -16px; /* toggle image */
}
A working example: http://jsfiddle.net/4CfzD/
Why not just use someone's plugin? e.g. http://www.fyneworks.com/jquery/star-rating/

Categories