Javascript Image roll over with onclick toggle - javascript

I currently have code to roll over an image link as below:
<a id="show" href="#"><img src="images/show-me.png" border=0 onmouseover="this.src='images/show-me_over.png';" onmouseout="this.src='images/show-me.png'"></a>
on clicking this it shows a div
jQuery(document).ready(function(){
jQuery("#show").toggle(
function(){
jQuery('#home-hidden-extra').animate({'height': '480px'} ,1000);},
function(){
jQuery('#home-hidden-extra').animate({'height': '0px'} ,1000);}
);
});
What i would like to do but i cant find/figure out is to use show_me.png and show_me-over.png when the div is hidden and then hide_me.png and hide_me-over.png when the div is shown.
How is the simplest way to achive this?
Thanks again!

You should be putting static hover styles to CSS. Define
.linkShow {
background-image: url("images/show_me.png");
}
.linkShow:hover {
background-image: url("images/show_me_hover.png");
}
.linkHide {
background-image: url("images/hide_me.png");
}
.linkHide:hover {
background-image: url("images/hide_me_hover.png");
}
Then add these classes to the link with jquery.
$("#show").removeClass().addClass("linkShow");

This ought to work:
HTML
<a id="show" href="#">
<img id="the-image" src="images/show-me.png" />
</a>
JS
$(document).ready(function(){
setupHover(); /* Defined below */
$('#show').toggle(function(){
$('#home-hidden-extra').animate({'height': '480px'} ,1000);
setupHover();
},function(){
$('#home-hidden-extra').animate({'height': '0px'} ,1000);
setupHover();
});
});
function setupHover(){
/* Unbind existing hover handlers */
$('#show').unbind('mouseenter mouseleave');
/* Use the correct image filenames depending on the situation */
if($('#home-hidden-extra').height() > 0){
images = ['show-me.png','show-me_over.png'];
}
else {
images = ['hide_me.png','hide_me-over.png'];
}
$('#show').hover(function(){
$('#the-image').attr('src','images/' + images[1]);
},function(){
$('#the-image').attr('src','images/' + images[0]);
});
}

Related

Change span on click Jquery

I've this code
$('a').click(function() {
$('a span').first().addClass('hide');
$('a span:nth-child(2)').removeClass('hide');
$('a span:nth-child(2)').addClass('display');
});
.hide {
display:none;
}
.display {
display:block;
}
<a href="#">
<span>Hello</span>
<br>
<span class="hide">World</span>
</a>
When I click on the link I want the first span hide and the 2nd span appears.
I want to do it multiple times (click on this link multiples times and have the same result).
I try to do it with this Jquery code
I would just toggle the classes
and you have to prevent the default on the link so it doesn't try to leave the page.
$('a').click(function(e) {
e.preventDefault();
$('a span').toggleClass('hide show');
});
here is a work demo
I would do something like this. Loop through the children of the a and swap their classes. Doing this method allows you to do other things as well as just toggle the class like adding text/css attributes to the span's.
$('a').click(function() {
$(this).children().each(function(index, element)
{
if ($(this).hasClass('hide'))
{
$(this).removeClass('hide');
$(this).addClass('display');
}
else
{
$(this).removeClass('display');
$(this).addClass('hide');
}
});
});
Here is a JS Fiddle of the code above
This should do what you are looking for:
HTML:
<a class='hide-clicker' href="#">
<span>Hello</span>
<span class="hide">World</span>
</a>
jquery:
$("a.hide-clicker").click(function(e) {
e.preventDefault();
$(this).find('span').toggleClass('hide');
});
CSS:
.hide {
display:none;
}

About click or hover functions

We have a code, when you click the icon, a div appears.
We want to make it appear when hover on icon, not click.
How can we do it?
This code is in my Wordpress theme's footer section.
It needs a change to work like i want. Code is this:
<script type="text/javascript">
$(document).ready(function(){
var searchi = 0;
$("#social-medias").click(function(){
$("#medias-icons").slideToggle("fast");
if ( searchi == 0 ){
$(this).addClass("searchAct1");
searchi = 1;
}else{
$(this).removeClass("searchAct1");
searchi = 0;
}
});
});
</script>
instead of .click(function() { //code here; }); use .mouseover(function() { //code here; });
then to get it to undo, use .mouseout(function() { //reverse code here; });
I see these elements are nested, so you can achieve this using only styles
.a{
color:blue;
}
.a .b{
color:red;
}
.a:hover .b{
color:green;
HTML:
<div class="a">
a
<div class="b">
b
</div>
</div>
Following this example, you can override nested classes behaviour(e.g. display:none/block) when cursor is over it's parent (hover)

scroll to the sibling element on click

Please take a look at this FIDDLE that shows and hides the text in a container on click . What I'm trying to do is that when I click open the first hidden text and then scroll down to click open another one, I want it to scroll back to the sibling image of that opened text to keep it in view. How can I find the sibling element and scroll to it on click?
This one is not valid.
$('.slider2').click(function(e) {
var imageposition = $(this).closest('.imageclass');
$(document.body).animate({scrollTop: imageposition.offset().top}, 'fast');
});
HTML:
<div class="container" style="border:2px solid #222;">
<img class="imageclass" style="width:100px;height:100px" src ="image.jpg">
<div class="slider2">Hi</div>
<div class="internal" style="display: block;">Text<p></p></div>
</div>
<div class="container" style="border:2px solid #222;">
<img class="imageclass" style="width:100px;height:100px" src ="image.jpg">
<div class="slider2">Hi</div>
<div class="internal" style="display: block;">Text<p></p></div>
</div>
..............
JS:
$('.slider2').click(function(e) {
e.preventDefault();
$(this).next(".internal").load($(this).data("ship"));
$('.internal').slideUp('normal');
if ($(this).next().is(':hidden') === true) {
$(this).addClass('on');
$(this).next().slideDown('normal');
}
var imageposition = $(this).closest('.imageclass');
$(document.body).animate({scrollTop: imageposition.offset().top}, 'fast');
});
$('.internal').hide();
You've at least a couple of problems here
$(this).closest('.imageclass') doesn't select the image that is previous sibling of <a>
even if you get your desired image, the moment your scrolling code runs, the image has not placed itself to its final position.
using $(document.body) to scroll the window (I'm doubtful about it myself)
Below code selects the right image element, gets the scrolltop at right moment, and scrolls the html, body using working syntax.
$(function () {
$('.slider2').click(function (e) {
e.preventDefault();
$(this).next(".internal").load($(this).data("ship"));
$('.internal').slideUp('normal');
var imageposition = $('.imageclass', $(this).closest('.container'));
if ($(this).next().is(':hidden') === true) {
$(this).addClass('on');
$(this).next().slideDown('normal', function () {
$('html, body').animate({scrollTop: $(imageposition).offset().top})
});
}
});
$('.internal').hide();
});
There's a bit of a problem with how your scrolling function works because the position of the active .container alters in relation to other containers(when active and inactive state).
Also, you should not be looking for the closest position but for its parent element.
Please take a look at my code: CSS
.slider2 {
margin:40px;
}
.internal p {
padding:5px;
}
.internal h3 {
text-align:center;
}
.container {
position: relative;
}
You might need to look for a way, to detect the height of an inactive container since I made mine as a static value.
JS:
$('.slider2').click(function (e) {
e.preventDefault();
$(this).next(".internal").load($(this).data("ship"));
var containerHeight = 205;
var containerIndex = $(this).offsetParent().index();
$('.internal').slideUp('normal');
if ($(this).next().is(':hidden') === true) {
$(this).addClass('on');
$(this).next().slideDown('normal');
}
var scrollPosition = containerHeight * containerIndex;
$('body').animate({
scrollTop: scrollPosition
}, 'fast');
});
$('.internal').hide();

Changing a image for another after clicking on it with jquery

this is going to be my first question so far cause i always do a research before using forums, but i dont get this to work.
I have an Image that works as a button for a toggle animation (button.png) and i want that image to change after clicking on it for another image (button2.png), and once you click the second image it changes again to the first image, i have:
<script type="text/javascript">
$(document).ready(function() {
// when click on the tag with id="btn"
$('#btn').click(function() {
// change the state of the "#idd"
$('#idd').toggle(800, function() {
// change the button text according to the state of the "#idd"
if ($('#idd').is(':visible')) {
$('#btn').attr('images/button2.png', this.href); // Show Less.. button
} else {
$('#btn').attr('images/button.png', this.href); //Learn More.. button
}
});
});
});
</script>
and my Html:
<div id="idd" style="display:none;">
- Here my hidden content -
</div>
<!-- Button -->
<img src="images/button.png" style="cursor: pointer;" id="btn">
What im doing wrong? Please Help :(
Check the syntax for .attr. It should be something like
$('#btn').attr('src', 'your image src');
Function Reference: http://api.jquery.com/attr/
To change the value of src you use 'attr' like this:
$('#btn').attr('src', 'images/button2.png');
Here is a DEMO
HTML
<div id="idd" class='display-none'>
- Here my hidden content -
</div>
<!-- Button -->
<img src="http://placekitten.com/40/40" id="btn">
CSS
.display-none {
display:none;
}
jQuery
var btn = $('#btn');
var idd = $('#idd');
btn.click(function() {
idd.toggle(800, function() {
// change the button text according to the state of the "#idd"
if (idd.hasClass('display-none')) {
btn.attr('src', 'http://placekitten.com/50/50');
idd.removeClass('display-none');
} else {
btn.attr('src', 'http://placekitten.com/40/40');
idd.addClass('display-none');
}
});
});
take one division e.g #play-pause-button and other in this i.e #play-pause. now put ur images in the src of inner division , on the click of outer divison source of innner division will change..
here is simillar example i'm working on. hope will help you.!
$('#play-pause-button').click(function () {
// $('#play-pause-button').play();
if ($("#media-video").get(0).paused) {
$("#media-video").get(0).play();
$("#play-pause").attr('src', "Image1 path");
}
else {
$("#media-video").get(0).pause();
$("#play-pause").attr('src', "Image2 path");
}
});
You should use css to associate image(s) on your "button" and a css class to determine which to show.
You can then use Jquery's ToggleClass() to add/remove the class. You can use the same class to show/hide your hidden content.
markup
<div id="idd">
- Here my hidden content -
</div>
<div id="btn">Click Me</div>
css
#idd.off {
display:none;
}
#btn {
border:1px solid #666;
width:100px; height:100px;
background:#fff url(images/button.png) no-repeat 0 0; // Show Less.. button
}
#btn.off {
background:#fff url(images/button2.png) no-repeat 0 0; //Learn More.. button
}
jquery
$('#btn').click(function(){
$('#idd').toggleClass('off');
$('#btn').toggleClass('off');
});

jQuery slider animate

I want to create a slider with jquery animate and I can't think of how to rotate and animate the next elements here is my HTML
<div id="slider">
<a href="http://somthing/" target="_blank">
<img src="http://something/logosponso.jpg" title="Another" />
</a>
<a href="http://somthing2/" target="_blank">
<img src="http://something/logosponso2.jpg" title="Another" />
</a>
…
…
I have the first a tag hidden in the css
#slider a{
display: none;
}
What I want to happen is after 4 seconds of delay I fading in or slide in with some animation the next a tag and hiding the current. I want to do this until I get to the last a tag then I want to circle back around to the first a tag
I have this so far and not sure if I am heading in the right direction…any ideas?
$("#slider a").fadeIn(500).delay(3000)
$("#slider a:first").show();
$('#slider a').click(function(e) {
e.preventDefault();
$(this).hide();
if ($(this).is('#slider a:last')) {
$('#slider a:first').fadeIn(500);
} else {
$(this).next().fadeIn(500);
}
});
I actually not understand what you need. However, if you want to show on page $(document).ready(..., you can try code bellow and modify as you need:
$(document).ready(function() {
$('#slider a').each(function() {
if ($(this).is('#slider a:first')) {
$(this).fadeIn(500);
} else {
if ($(this).prev().not(':hidden')) {
$(this).fadeIn(500);
}
}
});
});

Categories

Resources