Changing background image of img element with sprites - html

Trying to change the background image of a button using CSS, JS and jQuery. HTML first:
<button id='btn_home'>
<img class='home_green' src="images/menus.png">
</button>
Now the CSS:
.home_green {
background:url(images/menus.png) 0 0;
}
.home_blue {
background:url(images/menus.png) 0 -106px;
}
Here is the jQuery script. I don't think this is the problem because the console output is perfect:
$("#btn_home").find('img').hover(
function () {
console.log("mouse enter");
$(this).removeClass('home_blue');
$(this).addClass('home_green');
console.log($(this).attr('class'));
},
function () {
console.log("mouse leave");
$(this).removeClass('home_green');
$(this).addClass('home_blue');
console.log($(this).attr('class'));
}
);
I get no errors thrown, but no change to my background image, either. Pretty sure that I am defining something wrong in the relationship between my HTML and CSS, but I don't know what. Thanks for any help.
EDIT: Here is another attempt. When I try this, no image at all shows up. Everything else (CSS, jQuery) is the same:
<button id='btn_home' class='home_green'></button>

This can be done by CSS
you can add styles to button itself instead of img just add a width and height
<button id='btn_home'></button>
.btn_home {
background:url(images/menus.png) 0 0;
}
.btn_home:hover {
background:url(images/menus.png) 0 -106px;
}
div {
width: 89px;
height: 91px;
}
button {
background: url(http://kaioa.com/b/0907/sprite_eyecatcher.png) no-repeat;
width: 100%;
height: 100%;
border: 0;
background-position: -5px -4px;
padding: 0px;
cursor: pointer;
}
button:hover {
background-position: -94px -4px;
}
<div>
<button id='btn_home'></button>
</div>

Here is a working fiddle to accomplish what you want.
Correct, you can just set a background image to the <button> element, and remove the <img> inside.
Let me know if you have any questions.

Related

How can you modify and edit image colors on mouse hover?

See this visualization here where the color of the image is changed when you mouse hover the image:
http://thebandcalledboy.com/
I'm trying to replicate the same behavior. Any clues?
You can use the ":hover" pseudo-class to give a different style to an element that is hovered over. For example, if you have an element like this:
<div class="my-element">Content</div>
You can style the element differently when it is hovered over:
.my-element {
background: green;
}
.my-element:hover {
background: red;
}
The code above gives the element a red background on hover but a green background otherwise. You can use a similar technique (albeit with different CSS properties) to either select two different background images or to reuse the same image but apply a different color mask on top of it.
If you are using chrome, right on the image->inspect element. What you will get is this:
HTML:
</div>
<img src="sites/all/themes/boy/images/boy_logo.png" class="logo" alt=""/>
</div>
CSS:
element.style {
}
thebandcalledboy.com/media="all"
#center .black {
width: 96%;
height: 97%;
margin: 2%;
background: #000;
}
user agent stylesheetdiv {
display: block;
}
Inherited from body
Style Attribute {
font-size: 7.9375px;
}
thebandcalledboy.com/media="all"
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
font-family:'PerpetuaRegular',arial,sans-serif;
font-size: 20px;
}
Inherited from html.js.textshadow.fontface.audio.svg
thebandcalledboy.com/media="all"
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
font-family: 'PerpetuaRegular',arial,sans-serif;
font-size: 20px;
}
Similarly see
computed
and
event listeners there.
I think I would draw the colors onto a off screen PGraphic object and use something like the PImage blend function to mask it onto the template of the boy's face.

Rollover image in a div?

I have a custom twitter share button that is in the bottom right of my page. Its an image inside a div that is meant to rollover, but it wont rollover. Is it even possible to add rollover images inside a div?
so is there a workaround that will make the image rollover?
HTML:
<div id="twitter"><img src="Images/twitter_06.png" width="46" height="51" ></div>
CSS:
#twitter {
font-family: "Bebas Neue";
color: #000;
margin: 0 auto;
padding: 0;
right: 40px;
bottom: -12px;
position: fixed;
z-index: 100;
font-size: 30px;
}
If this is a simple image rollover don't use JS for this use CSS.
a.twitter {
display: block;
cursor: default;
width: 400px;
height: 300px;
background: url('images/twitter.png') top left;
}
a.twitter:hover {
background-position: center left;
}
a.twitter:active {
background-position: bottom left;
}
Mark it up in HTML like so...
<div class="twitter">
</div>
You can do that with javascript.
<div class="twitter"><img src="1.png"/></div>
<script type="text/javascript">
var t = document.getElementsByClassName("twitter")[0];//i only have one element with that tag so...
//if you are going to put more you can loop through them
prepareRollOver(t);//call the function to prepare the rollover
function prepareRollOver(target){
target.onmouseover = function (){
img = this.getElementsByTagName("img")[0];//only one img expected, so take the first of the array
img.src="src1.png"
}
target.onmouseout = function (){
img = this.getElementsByTagName("img")[0];//only one img expected, so take the first of the array
img.src="src2.png"
}
}
</script>
You can also do this with css like this
.twitter{
background:url("1.png");
}
.twitter:hover{
background:url("10.png");
}
But remove the img tag inside the div, you don't need it if you are going to use css
Change src1.png and src2.png to match your file names.

Confused with CSS: image button with hover tooltip and image changing when hover

I understand the basics of CSS.In fragments.
I try to understand how to combine all of what I want together.
I want to create two types of buttons, "view" and "save".
I have 4 images, two for each, where one represents the button, and one when mouse hovers it.
So I want an alternative background when hover.
I want also a tooltip, something "cheap" like "View file", "Save file", that better explains the content of the image if it wasn't clear enough.
I understand that I need to use the hover attribute, background attribute, and somehow to create a tooltip.
Those two buttons are actually images with a link
How do I combine it all together?
you could do something like this:
<div title="View File" class="view-button">
View File
</div>
CSS:
.view-button {
background-image: url("first-view-image");
}
.view-button:hover {
background-image: url("second-view-image");
}
And the same for the save button. The title tag shows a small tooltip. if you want bigger/fancier tooltips, you have to use some javaScript framework.
check this out
HTML
<button>Hello</button>
CSS
button {
background: url(your-image.png) no-repeat;
}
button:hover {
background: url(your-image-hovered.png) no-repeat;
}
button:focus {
background: url(your-image-focused.png) no-repeat;
}
Hi i've created a fiddle for you. When you hover the image/button changes and also you get a small tooltip that says "Click here to Save". Is that all you need? Tell me if this helped you or you have any questions.
EXAMPLE
HTML
CSS
.save {
margin-bottom: 10px;
width: 47px;
height:20px;
display:block;
background:transparent url('http://www.mtsworld.com/images/button_submit.gif') center top no-repeat;
}
.save:hover {
background-image: url('http://www.ecdoe.gov.za/ecdoe/graphics/buttons/submit.png');
}
This is my example, change background value to your image, to use tooltip i used jqueryui
Example
html
<a href='#' class='button savebutton' title='Save form'>Save</a>
<a href='#' class='button cancelbutton' title='Cancel action'>Cancel</a>
css
.button{
background : #999;
padding : 5px 10px;
border-radius : 5px;
color:#fff;
text-decoration : none;
}
.savebutton{
background : blue;
}
.cancelbutton{
background : orange;
}
.savebutton:hover{
background : cyan;
}
.cancelbutton:hover{
background : yellow;
}
.ui-tooltip {
padding: 8px;
position: absolute;
z-index: 9999;
max-width: 300px;
-webkit-box-shadow: 0 0 5px #aaa;
box-shadow: 0 0 5px #aaa;
}
body .ui-tooltip {
border-width: 2px;
}
JS
$('.button').tooltip();

Clickable image with sprites

So I've got a series of clickable images in my page. I've tried to optimise this by generating a single image containing all the images I need and I intend to use sprites to select the one I want. I'm having trouble figuring out the best way to add anchor tags to the sprites though?
So I'm after a clickable HTML element that supports sprites, preferably without using JavaScript. I can do it using JavaScript but I'd prefer to avoid it.
OK, here's my code, what there is:
.touringEscorted {
height:125px;
width: 214px;
background-image: url('/Images/Travel2New/ToursImages/ToursBanners.jpg');
background-position: 0 0;
}
.touringNew {
height:125px;
width: 214px;
background-image: url('/Images/Travel2New/ToursImages/ToursBanners.jpg');
background-position: -10px 0;
}
I've tried
<div class="touringEscorted">
and
and several others. Seems there's no way to use sprites/background images and anchor tags at the same time. Am I right?
Any suggestions?
Ok then :
Should work, but adding display:block; to the CSS :
.touringEscorted {
height:125px;
width: 214px;
background-image: url('/Images/Travel2New/ToursImages/ToursBanners.jpg');
background-position: 0 0;
display:block;
}
Like this?
<a class="sprite sprite1" href="javascript:;">Link Text</a>
sprite {
display: block;
background: url(path/to/image/file.ext);
text-indent: -9999px;
}
sprite1 {
width: WWpx;
height: HHpx;
background-position: -NNpx - MMpx;
}
Doesn't Google consider off screen text as spammy? I came up with a modification. I put the link in another element, in this case a table. I added the background image class in the element and in the link like this:
CSS code:
.sprite{
background: url('images/sprite.png') no-repeat top left;
}
.sprite.termite {
background-position: 0px -499px;
width: 150px; height: 113px;
display: block;
}
HTML code:
<td class="td sprite termite">
</td>
It renders the image in the table perfectly and clicks!

a:hover background replacement on a empty a tag not working

Im using hyperlinks as a image, well a background image so that I can do a image swap eaisly with a:hover.
I have the following:
<a class="cross" href='#'></a>
And the following css
a.cross {
background:transparent url(/images/cross-grey.png) no-repeat scroll 0 0;
float: right;
border: none;
width: 19px;
height: 19px;
display: block;
}
a:hover.cross {
background-position: 0 20px;
}
This works fine in Firefox but not in IE6. Is this a issue with IE6 and a simple css fix or is there a better way of implementing what I am doing. Thanks.
Change:
a:hover.cross { }
To:
a.cross:hover { }
I think you should deal the a:hover.cross with javascript.
Regards.