MouseEnter and MouseLeave on an Image - javascript

I've seen this question before, but none of the solutions I've seen hence far have worked.
I'm attempting to simply get the image to fade in when hovered over and fade out when not. The following is a snippet of my HTML:
<script type="text/javascript" src="js/tabs.js"></script>
<ul>
<li>
<a href="#jenn">
<img class="img-fade" src="img/team/jenn.jpg" alt="Jenn">Jenn
</a>
</li>
</ul>
I have some CSS that sets .img-fade to opacity: 0.5;.
Then my tabs.js file looks like this:
$(document).ready(function() {
$('.img-fade').mouseenter(function() {
$('.img-fade').fadeTo('fast',1);
});
$('.img-fade').mouseleave(function() {
$('.img-fade').fadeTo('fast',0.5);
});
});
I'm not sure why this isn't working. It seems simplistic enough. Any help would be much appreciated. Thanks!

I tried your code with the following reference to jquery in the header:
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
And it worked well! Are you still having issues?

Related

Javascript function works on JSFiddle but not in my HTML document?

So, I found a code/function I wanted to use on this site here: stackoverflow
I then copied the code from the second answer, did some small changes and tested if it actually worked, and I found out it did not. All of the functions from the link work on JSFiddle tho, but none of them work for me in my html document.
I did < script>, didn't work. I tried to make a separate .js document, but the code was still not working.
<body>
<div id="bokse2"></div>
<div id="boksi"></div>
<script src="test.js" type="text/javascript"></script>
<script>
$(function() {
$('#bokse2').click(function() {
$('#boksi').css('margin-left', '-=10px');
});
});
</script>
</body>
The big box (boksi) should move 10 pixels to the left by clicking on the smaller box (bokse2). Just like it does here: JSFiddle
You are missing the include to the jQuery
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">

JQuery UI - .resizable not working

I have looked around and although I have found similar questions to this one, none of them had any solutions that worked for me.
Here is a link to another question similar.
Draggable and resizable in JqueryUI for an image is not working?
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
</head>
<body>
<div id="draggableHelper" style="display:inline-block">
<img id="image" src="http://www.google.com.br/images/srpr/logo3w.png" />
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#draggableHelper').draggable();
$('#image').resizable();
});
</script>
</body>
</html>
This is just a very basic example but when I run this the image is movable but is not resizable. Although as far as I can tell, it should definitely work.
In the link above at the bottom of the question there is a link to a working example.
http://jsfiddle.net/8VY52/
The example is using jfiddle with this exact same HTML and javascript.
Is there something I am missing about Jquery UI, why does this work through Jfiddle but does not seem to work within the code above.
Thanks.
You are missing the jquery-ui CSS file in your code
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css"/>
Demo: Plunker
Complete working code would be.
</head>
<body>
<div id="draggableHelper" style="display:inline-block">
<div id="image"><img src="http://www.google.com.br/images/srpr/logo3w.png" /></div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#image').resizable();
$('#image').draggable();
$('#image').resize(function(){
$(this).find("img").css("width","100%");
$(this).find("img").css("height","100%");
});
});
</script>
</body>
</html>
This will work for you.
<div id="draggableHelper" style="display:inline-block">
<div id="image"><img src="http://www.google.com.br/images/srpr/logo3w.png" /></div>
</div>
As the resizable property only works on right side and bottom side so find the image borders find that by selecting a image in css and border to it then see the output it will work perfectly

Can someone explain behind the scenes why this Jquery Hover image command doesnt work?

Can someone explain behind the scenes why this Jquery Hover image command doesn't work?
I have seen working examples but I want to know WHY this doesn't work. (improper syntax is not the answer I am looking for)
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript"></script>
<script>
$(document).ready(function() {
$("#banner").fadeIn(1500,function(){
$("#contactBt").fadeIn('slow');
});
$("#contactBt").hover(function(){
$("#contactBt").css("src","images/contactBtHover.png");
},function(){
$("#contactBt").css("src","images/contactBtNoHover.png");
});
});
</script>
</head>
<body bgcolor=212121>
<div style="margin-top:15%; margin-left:10%" >
<img id="banner" style="display:none;" src="images/banner.png" width=1000 height=275/>
</div>
<img id="contactBt" style="display:none; margin-top:0%; margin-left:10%" src="images/contactBtNoHover.png" width=250/>
</body>
Basically I just want it to say when the mouse is hovering over the image change its image source to a different image. Vice Verse on the way back
That would be:
$("#contactBt").attr("src","images/contactBtHover.png");
as src is an attribute, not a style.

ul Component Won't Hide

I've done a simple menu using some HTML, CSS and Javascript. The main idea is that it should be hided until the user click on it, but the problem is that it won't start the page hidden and nothing happens when I click, just like if the Javascript won't active. Here is part of my files:
index.html
<head>
<link rel="stylesheet" type="text/css" href="style/sample.css" />
</head>
<body>
<script src="js/menu.js" type="text/javascript"></script>
<div id="container">
<div id="header">
<center>
<div class="leftMenu" onclick="toggleMenu()">Menu</div>
<h1>Test</h1>
</center>
<ul>
<li>About</li>
<li>Blog</li>
<li>Contact</li>
</ul>
</div>
</div>
</body>
menu.js
$(document).ready(function() {
$('#header ul').addClass('hide');
$('#header').append('<div class="leftMenu" onclick="toggleMenu()">Menu</div>');
});
function toggleClass() {
$('#header ul').toggleClass('hide');
$('#header .leftMenu').toggleClass('pressed');
}
sample.css
#header ul.hide {
display: none;
}
#header div.pressed {
-webkit-border-image: url(graphics/button_clicked.png) 0 8 0 8;
}
What I'm making wrong and how I can correct it?
I think at least part of the problem is that the menu toggle div you're creating is using the function toggleMenu() and not toggleClass().
EDIT: I made a jsfiddle that shows the changes I would propose to make it work properly: http://jsfiddle.net/avidal/dDDKz/
The key is to remove the onclick attributes, and use jQuery to handle the event binding for all current, and future matching elements:
$(document).ready(function() {
$('#header ul').addClass('hide');
$('#header').append('<div class="leftMenu">Menu</div>');
$('#header div.leftMenu').live('click', toggleClass);
});
function toggleClass() {
$('#header ul').toggleClass('hide');
$('#header .leftMenu').toggleClass('pressed');
}
Ok, a few things:
Make sure you have a doctype. You said this was just part of your code, so perhaps you do, but just to be sure, I'll point this out anyway. You can just use the html5 doctype (it is compatible with IE6 if that's a worry):
<!DOCTYPE html>
<html>
<head>...head stuff here </head>
<body> ...Body stuff here</body>
</html>
Make sure you are loading the jquery library. Again, maybe you already are, but just making sure your bases are covered here. I'd suggest putting it in your HEAD. You should also move menu.js out of the head and just below the closing BODY tag.
If you want the UL to start off hidden, you should have the default CSS for it display none. Otherwise, even if your script was working, you would see the ul for a moment before it became hidden as the page would first load and then the JS would apply the class. In that gap between page loading and JS applying your class, the UL will be visible.
Your scripts could really be optimized, and I apologize for not being more specific (shouldn't write these when I'm trying to get ready for bed), but I'll at least point out the most obvious fix here - make sure that you are calling the correct method. I see that in the HTML snippet you are appending, you are calling toggleMenu(), but in your actual JS, your function is called toggleClass. Change one of those so they match.

Simple Jeditable Problem

I just started with Jeditable and for 3 hours now it seems I can't figure it out. This tutorial should have been piece of cake:
http://www.appelsiini.net/projects/jeditable
, but it turned out to be a little pain in the a$$. I've put the jquery.js and jquery.jeditable.js in the same directory with the main page. This is my code (it seems like the code tags won't do the trick, so I'll give you only the important blocks):
the header contains
<script type="text/JavaScript"
src="jquery.js"></script>
<script type="text/JavaScript"
src="jquery.jeditable.js"></script>
<script type="text/JavaScript"
$(document).ready(function() {
$('.edit').editable('#');
});
and the body of my html contains:
<div class="edit" id="div_1">Edit me</div>
And that's about it. It should give me an editable form when I click the "Edit me", but nothing happens. Where do I go wrong ? Thanks in advance.
I don't know if this is a typo in the question or your actual code but check this line:
<script type="text/JavaScript"
$(document).ready(function() {
$('.edit').editable('#');
});
it should be
<script type="text/JavaScript">
$(document).ready(function() {
$('.edit').editable('#');
});
</script>
Are you trying to send the ajax to the same page you are on? If so, replace the '#' with window.location.href and you should be good to go.

Categories