I am trying to write a menu for a front page that when an item is hovered over, the background image of the background div changes.
I have three main documents creating this effect, menuFunction.php, which is included in the header file, so it is not a link problem, then there is index.php, and finally my stylesheet.
Here is an example of one of the menu items functions from my menuFunction.php file:
<script>
$(document).ready(function () {
$(".resume").hover(function () {
$(".backgroundImage").css("background", "url(http://www.vhawley.com/wp-content/themes/vhawleycomtheme/css/style/images/menuResume.jpg)");
});
$(".resume").mouseleave(function () {
$(".backgroundImage").removeAttr('style');
});
});
</script>
Now here is where it is mentioned in index.php:
<div id="photoBackground" class="backgroundImage">
<div id="menuWrap">
<ul id="menu">
<li id="menuResume" class="resume">Resume</li>
<li id="menuAbout" class="about">About</li>
<li id="menuGallery" class="gallery"><a href="http://www.vhawley.com/gallery">Gallery</li>
<li id="menuContact" class="contact"><a href="http://www.vhawley.com/contact">Contact</li>
</ul>
</div>
</div>
Notice that the classes mentioned in the menuFunctions.php are also mentioned in the corresponding li.
Now, I am able through my stylesheet to define that the background have the following attributes for #photoBackground
#photoBackground{
height:450px;
width:100%;
background-image:url(http://www.vhawley.com/wp-content/themes/vhawleycomtheme/css/style/images/menuDefault.jpg);
background-repeat:no-repeat;
background-position:center;
}
Lastly, what I am now trying to accomplish is to get the rest to work, but even when I use the class .resume in my CSS, it does not work, even when I use the div #menuResume, no luck. I have been trying forever and have tried putting off asking for help on this one, but I really am stuck. I have really no JQuery experience, just CSS, HTML 5, and some PHP, which usually if you can master them, they can make a decent site alone. Until you want to get fancy like this.
Any ideas?
EDIT:
It is an issue with linking the JQuery variables to CSS, it has nothing to do with getting the background to change, because as Popnoodles proved on JSFiddle, nothing is wrong with that.
You could do this with the css :hover selector. However, if you want to use jQuery, you need to change .css('background'... to .css('background-image'...
It is the small mistakes that trip us up the most.
Edit to clarify your mistake:
The background style overrides everything, including background-repeat and background-position so those are all gone. If you used background-image it wouldn't override those other css options.
<script>
$(document).ready(function () {
$(".resume").hover(function () {
$(".backgroundImage").css("background-image", "url(http://www.vhawley.com/wp-content/themes/vhawleycomtheme/css/style/images/menuResume.jpg)");
});
$(".resume").mouseleave(function () {
$(".backgroundImage").removeAttr('style');
});
});
</script>
Best way to do this would be plain css
.resume #photoBackground {
...
}
.resume:hover #photoBackground {
... new image
}
I would research spriting to save some image loads, but that would requeire a fixed or known hight for the background image container. I would also use the following instead of wiring up a handler for each menu item:
HTML
<div id="photoBackground" class="backgroundImage">
<div id="menuWrap">
<ul id="menu">
<li id="menuResume" class="resume">Resume
</li>
<li id="menuAbout" class="about">About
</li>
<li id="menuGallery" class="gallery">Gallery
</li>
<li id="menuContact" class="contact">Contact
</li>
</ul>
</div>
</div>
Script
//Save looking up background container each time
var background = $("#photoBackground");
$("#menu li").hover(
//Hanldes Mouse Enter
function () {
var cssClass = $(this).attr('class');
$(background).addClass(cssClass);
},
//Handles Mouse Exit
function () {
var cssClass = $(this).attr('class');
$(background).removeClass(cssClass);
})
CSS
.backgroundImage {
background-image:url(http://www.vhawley.com/wp-content/themes/vhawleycomtheme/css/style/images/menuResume.jpg);
}
.backgroundImage.resume {
background-position:left -100px;
}
.backgroundImage.about {
background-position:left -200px;
}
.backgroundImage.gallery {
background-position:left -300px;
}
.backgroundImage.contact {
background-position:left -400px;
}
This also kind of demonstrates the principle of spriting by just changing the offset of the background image. OF course you could always just change the background image itself.
Demo
Related
I have a .php file in which I have a layout that I would like to change depending of a div's width. That div is a parent of the following code and is situated in a other file.
<?php if ($(".container").width() > 400) { ?>
<div class="sub-container">
<div">sidepanel-left</div>
<div>content</div>
<div>sidepanel-right</div>
</div>
<?php } else { ?>
<div class="sub-container">
<div>sidepanel-left
<div>sidepanel-right</div>
</div>
<div>content</div>
</div>
<?php } ?>
That gives me a blank page with only Html, head and body tags.
I'm sure it's a pretty basic code that I need, but I'm just starting in PHP/JS and I'm not sure of what I'm doing -_-
Let me know if you need more information!
Thanks in advance for the help! :)
B.
You can not do this in PHP. What you can do is the following:
From PHP side, output this HTML code:
<div class="container">
...
<div class="sub-container">
<div>sidepanel-left</div>
<div>content</div>
<div>sidepanel-right</div>
</div>
</div>
Then in your HTML page, make sure jquery is loaded, and put this:
<script type="text/javascript">
$(document).ready(function() {
$('.container').each(function(){
var $this = $(this);
if ( $this.width() > 400 ) {
$this.addClass('wide-container');
}
});
});
</script>
Then, in your css file you can setup a class for regular container and wide container, and you can also control the styles of subcontainers:
.container {
// default styles here
}
.container.wide-container {
// override wide container styles
}
.container.wide-container > .subcontainer {
// override subcontainer styles
}
You might use #media queries to achieve similar result, but #media queries can be applied according to the width of the screen, not a particular container.
So I have a layout system that I am working on that allows users to click on links on a side menu to access some services.
So basically here is the simple html design
<div class="col-md-2" id="admin_menu">
<section class="list-group">
opt 1
opt 2
opt 3
Opt 4
</section>
</div>
<div class="col-md-10" id="admin_content">
<section class="">
Some content
</section>
</div>
The layout is working as i want it to. A small menu to the left and the main content to the right.
But I have a small issue with the design. As you can see the content is all on one page. When the user clicks on the opt2,3,4 links, he or she will be redirected to the respective page which will also contain the side menu.
So my questions are
What is the most convenient way/ways to create such a design whose
menu is needed in more than one interface.
How do i make the active link change color considering that most
likely the whole page will reload.
Here is the image for the design. The first link shows the style that the active link should have
include "menu.php";
And menu.php contains the menu.
Also works with
require_once "menu.php";
For question two, give each item a unique numbereid (listitem1, listitem2...) and on the page itself create a style tag:
<style>
#listitem1 {
whatever...
}
</style>
Depending on the page.
You can try this code i arranged for you:
1)first you can add some id in your code to identify in a unique way each link
2) second you just add some jquery code in the end of the body of the html code
the idea is that when you send the mouse over the link it changes color you can specify the color by yourself and when the mouse is out it changes color again try that i did it only for the first 3 links but you can do it for many link when you get the idea.
<div class="col-md-2" id="admin_menu">
<section class="list-group">
opt 1
opt 2
opt 3
Opt 4
</section>
</div>
<div class="col-md-10" id="admin_content">
<section class="">
Some content
</section>
</div>
<script>
$(document).ready(function(){
$("#first").mouseover(function () {
$("#first").css({
'color':'red'
});
});
$("#first").mouseout(function () {
$("#first").css({
'display':'lightgray'
});
});
});
$(document).ready(function(){
$("#second").mouseover(function () {
$("#second").css({
'color':'blue'
});
});
$("#second").mouseout(function () {
$("#second").css({
'display':'lightgray'
});
});
});
$(document).ready(function(){
$("#third").mouseover(function () {
$("#third").css({
'color':'yellow'
});
});
$("#third").mouseout(function () {
$("#third").css({
'display':'lightgray'
});
});
});
</script>
As #Faried said, you can include the menu into the main page.
And as for the second question you can use
<?php if(isset($_GET['page']) && $_GET['page']=="pageName")
{
//style for active link
} else
{
//non-active style
}
Hoping someone can point me in the right direction to an annoying problem.
To summarize before I put the code; I have two files.. a html file with javascript called "bookings.php", and a php file with php "getBookings.php".
The bookings.php file contains all the static information like menus, text etc along with javascript + ajax calls.
I'm calling the getBookings file successfully with javascript/ajax and works well and i don't have any problems with it.
My problem is, that from the file that is loaded (getBookings.php); there are some html elements that i need to toggle. The jQuery for it, I have included in the bookings.php file.. but the jquery won't apply or work when the ajax file is called.
All the contents on getBookings.php gets loaded into an #bookingSummary element.
Now, my code is very long, so i will include all the important parts..
// Usual Head Content, Meta, CSS, jQuery files
// Filters
<script language="javascript" type="text/javascript">
function bookings()
{
var fltr="";
if(document.frm.bkid.value != "")
fltr+='bkid='+document.frm.bkid.value+'&';
if(document.frm.customer.value != "")
fltr+='cid='+document.frm.customer.value+'&';
if(document.frm.site.selectedIndex != 0)
fltr+='sid='+document.frm.site.value+'&';
if(document.frm.status.selectedIndex != 0)
fltr+='st='+document.frm.status.value+'&';
if(document.frm.supp.selectedIndex != 0)
fltr+='sup='+document.frm.supp.value+'&';
if(document.frm.datefrm.value != "")
fltr+='dfrm='+encodeURI(document.frm.datefrm.value)+'&dto='+encodeURI(document.frm.dateto.value);
document.getElementById('bookingsummary').innerHTML="<br /><p align='center'> <img src='img/bigloading.gif'></p>";
url='get/getBookings.php?'+fltr;
getAjaxRec(url, viewbookings);
}
function viewbookings()
{
if (xmlHttp.readyState == 4 && (xmlHttp.status==200 || window.location.href.indexOf("http")==-1))
{
document.getElementById("bookingsummary").innerHTML = xmlHttp.responseText;
}
}
</script>
The Above code is the javascript that all works fine.. the following is the html code that calls the above function - bookings():
<!-- Several Form fields are here..-->
<input type="button" value="Apply Filter" class="searchBtn" onClick="bookings();">
<!-- Show booking results -->
<div id="bookingsummary"></div>
Now, within the getBookings file there is an html element with a class name "hiddenDetails" that i need to toggle and the Jquery for it is:
<script type="text/javascript">
$(document).ready(function() {
$('.bookingKeyDetails').click(function()
{
$(this).next('.hiddenDetails').slideToggle('medium");
});
});
</script>
The above code all works apart from the jquery.. here is a snippet from the getBookings.php file.. the part i want to apply the jquery to..
<?php
// A while loop happens then..
$result="
<div class=\"bookingKeyInfo\">
<div class=\"lb1\"><input type=\"checkbox\" name=sup1x".$rw['id']." ></div>
<div class=\"lb2\">".$rw['id']." </div>
<div class=\"lb3\">".date('d/m/Y', strtotime($rw['pickupDate']))." <strong><font color=\"#FF0000\">".date('H:i', strtotime($rw['pickupTime']))."</font></strong></div>
<div class=\"clearLeft\"></div>
</div>
<div class=\"bookingKeyDetails\">
<div class=\"b-one\"><img style=\"margin-top:9px; padding-bottom: 5px;\" src=\"$base_dir/img/".strtolower($rw['status']).".png\"></div>
<div class=\"b-two\"><img id=\"isCustomerEmailed\" style=\"margin-top:9px; padding- bottom: 5px;\" src=\"$base_dir/img/unassigned.png\"></div>
<div class=\"b-three\"><img id=\"isSupplierEmailed\" style=\"margin-top:9px; padding- bottom: 5px;\" src=\"$base_dir/img/unassigned.png\"></div>
<div class=\"b-four\"><img id=\"isSupplierConfirmed\" style=\"margin-top:9px; padding- bottom: 5px;\" src=\"$base_dir/img/unassigned.png\"></div>
<div class=\"b-trip\">".substr($rw['DA'],0,6)."..<strong> to </strong> ".substr($rw['DB'],0,6)."..</div>
<div class=\"b-pax\">".$rw['pax']." Pax</div>
<div class=\"b-cost\">€$cost </div>
<div class=\"b-supplier\">".substr($sup,0,10)."..</div>
<div class=\"clearLeft\"></div>
<div class=\"hiddenDetails\">More Booking Information...</div>
</div>";
echo $result;
?>
I know this is a very long description of the problem.. so I have tried to give my code exactly as it is..
Could anyone shed some light, as to why the jQuery isn't working.. cz it works separately, just not on any element that is called from getBookings..
The problem is, you're binding an event handler with jQuery to bookingKeyDetails before it actually exists on the page. Remember that this element is being added to your html only after the page is loaded, so any actions you make in $(document).ready(function() { will not apply to it.
Bind the event handler only after you change the html:
function viewbookings()
{
if (xmlHttp.readyState == 4 && (xmlHttp.status==200 || window.location.href.indexOf("http")==-1))
{
document.getElementById("bookingsummary").innerHTML = xmlHttp.responseText;
$('.bookingKeyDetails').click(function()
{
$(this).next('.hiddenDetails').slideToggle('medium');
});
}
};
Since the HTML element you need to toggle was never on the page when the calling page instantaiate the jQuery to toggle, you need to reinitialize the function after a successful call from your ajax.
You can use jQuery .success callback function for their AJAX call tools... I use that to help with this kind of processing instead of native AJAX. Makes life simpler and better cross browser support.
Just call your toggle function after the success.
http://api.jquery.com/jQuery.post/
It's very obvious that it will not toggle because you have writtent the Jquery in the "bookings.php". During the page load of bookings.php, it will not find any content of the getBookings.php so that Jquery will not apply as no element is available with that id.
You have to bind the event for the toogle on Ajax success event of the when you are calling for getBooking.php.
$.ajax({
url: URL,
success: function(){
$('.bookingKeyDetails').click(function()
{
$(this).next('.hiddenDetails').slideToggle('medium");
});
},
error: function(){
alert('failure');
}
});
You're using single and double quotes in your function call
$(this).next('.hiddenDetails').slideToggle('medium");
user 'medium' or "medium"
I have this div tags :
<div id="target-share">
<span class="to-admin">Admin</span>
<ul id="select-shareto">
<li data-val="0-1">
<span class="to-Finance">Finance</span>
</li>
<li data-val="1-1">
<span class="to-admin-private">Admin Private</span>
</li>
<li data-val="1-0">
<span class="to-ceo">CEO</span>
</li>
<li data-val="0-0">
<span class="to-ceo-private">CEO Private</span>
</li>
</ul>
<input id="shareto" type="text" value="0-1" name="shareto">
</div><!-- #target-share -->
and this JavaScript :
<script type="text/javascript">
$(document).ready(function($) {
$('ul li').click(function() {
$('input#shareto').val($(this).data('val'));
});
});
</script>
that JavaScript actually works when I'm using it with that div alone. but when I put it on my full code, that JavaScript doesn't work. I think that because there are more than one UL and LI tags on my code.
Now, the question is... how to apply that Javascript so that it can works ONLY for that div, even though there are other UL and LI tags.
Just use this instead targeting the div first then its contents
$('#target-share ul li')
JavaScript (not just Jquery) is all about scoping:
$('ul li').click(function() {
Causes that click to bind to every li in a ul so what you wanna do is scope the click down to a specific area of your page or set of elements across the page.
To scope it down to a specific element the best idea is to use a id like so:
$('#target-share ul li')
But to scope it down to a number of elements it is better to use a class like:
$('.target-share ul li')
Edit:
Also on() could be a good replacement here for click but it depends on where the HTML is being sourced and how it is being used, but thought I would make you aware of that function in case you didn't already know about it.
Select those ul li that have your data-val attribute:
$(function($) {
$('ul li[data-val]').click(function() {
$('input#shareto').val($(this).data('val'));
});
});
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/