How to add a fade-in effect in jQuery [duplicate] - javascript

This question already has answers here:
Transitions on the CSS display property
(37 answers)
Closed 2 years ago.
How to add a fade-in effect when clicking on a div. I have a profile menu dropdown and the click event was made with jQuery. Also, how to make this to close if I click anywhere outside?
This is the code
jQuery(document).ready(function(){
jQuery(".profile_link_header").click(function(){
jQuery(".dropdown").toggleClass("active");
})
});
CSS
.dropdown.active{
display: block;
}

Without your html code, you just add fade-in on your selector:
$(document).ready(function(){
$(".profile_link_header").click(function(){
$(".dropdown").toggleClass("active").fadeIn("slow");;
})
});
I suppose here is on your .dropdown class, else you apply the faeIn on your selector desired

Related

Jump to different sections of the page when theres a fixed navbar [duplicate]

This question already has answers here:
offsetting an html anchor to adjust for fixed header [duplicate]
(28 answers)
Closed 6 years ago.
So I have successfully gotten my page to jump to different sections on the page via buttons referencing the ID's like this
<button id="niftyBtn">Things</button></br>
<div id="nifty">Blah</div>
I have several of those and they all go to the correct portion of the page.
The problem is, there is a fixed nav that is included into the page that I can't change and the top of the sections are going behind that navbar instead of below it.
I have tried to use several different variations of a jquery solution I saw using .offset() but none of them have been effective. Any input is appreciated
Note: It behaves the exact same way when I use jQuery or javascript to jump the sections instead of href tags.
You don't need a javascript solution, something as simple as this will do:
h2:before,h3:before,h4:before {
content: " ";
display: block;
visibility: hidden;
margin-top: -7em;
height: 7em;
}
Granted, these are meant for headings, but you can substitute with a class, or IDs for the same effect. The measurements will have to be amended, of course.
BTW, why are you using <button> to wrap an <a>? That code should just be Things</br> and if you want it to look like a button, style it like a button.

How to reload or rerender CSS for a single HTML element? [duplicate]

This question already has answers here:
How can I remove a style added with .css() function?
(21 answers)
Closed 6 years ago.
Let's say I have a div.foo with this CSS:
.foo {
height: 70vh;
}
And I change the height of div.foo with jQuery like this:
$('.foo').height(100);
How do I reset the CSS of div.foo so its height is rendered 70vh again?
One solution that works is to reload the entire CSS file this way. But I wonder if there is a solution to only reset the CSS of div.foo, not the entire stylesheet?
With jQuery, you are adding your styles to the style attribute of the element. It is removed again, if you call the function again with an empty string (""). This will remove the property in the style attribute.
If you have e.g. this command:
$('.foo').css('height', '100px');
You can revoke it by
$('.foo').css('height', '');
See also the discussion here: https://stackoverflow.com/a/4036868/3233827
$('.foo).height(''); should be sufficient to erase the style applied by jQuery
You can remove the style attribute from the element. This attribute holds any dynamic styles.
$('.foo').css('background', 'teal');
$('#reset').click(function() {
$('.foo').removeAttr('style');
});
.foo {
height:100px;
width:100px;
background:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<div class="foo"></div>
<p><button id="reset">Reset style</button></p>

jQuery's `.hover()` method doesn't work for `:hover` CSS style? [duplicate]

This question already has answers here:
Trigger css hover with JS [duplicate]
(4 answers)
Closed 7 years ago.
Here is my code
http://jsfiddle.net/8fsmcc7b/
And I pasted it below:
setTimeout(function(){$("div").hover()}, 1000)
div {
background: grey;
width: 20px;
}
div:hover {
width: 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
test
</div>
I want to use jQuery to trigger the hover event of <div>, which will expand its width to 200px.. However, in the example above, there is no effect at all..
Does anyone have ideas about this?
You can expand it by this simple way.
Create a CSS class with the 200px width (here you can add also CSS3 animations). Use .hover(handler1, handler2) function from jQuery or .mouseenter() / .mouseleave(). The first handler for hover function, will be for the mouse entering that element, and just add the class, and the second element for the mouse leaving it, so remove the class added before.
The first one mentioned, is like the two mouse events from before.
Edit to add example:
$(".div_to_expand").hover(function() {
$(this).addClass("expanded");
}, function() {
$(this).removeClass("expanded");
});
Hover binds mouseover and mouseout handlers! I Don't think It triggers mouseover event.
If you want to apply a style you can do it by adding a class like bellow
div:hover,div.hoverd {
width: 200px;
}
<script>
setTimeout(function(){
$("div").addClass('hoverd');
setTimeout(function(){
$("div").removeClass('hoverd');
},1000)
}, 1000)
</script>

Css in Jquery with after and before [duplicate]

This question already has answers here:
Selecting and manipulating CSS pseudo-elements such as ::before and ::after using javascript (or jQuery)
(26 answers)
Closed 8 years ago.
Why does this snippet not work on a button? How can I do it correctly?
$('div.button:after').css({
display:'none'
})
You cannot select pseudo elements using inline CSS, think like the the way you cannot write :hover styles inline.
What jQuery does with .css() is it injects the styles inline so you cannot change a pseudo element using $('div.button:after').css().
The simple way to achieve that is by using say .addClass() method..
HTML
<div class="button">Hello</div>
CSS
.button:after {
content: " Hide this using jQuery";
color: red;
}
.hide_btn_pseudo:after {
display: none;
}
jQuery
$('div.button').addClass('hide_btn_pseudo');
Demo
Inorder to show the element, you can remove the class back, using .removeClass()
Actually you can't. Because it is a pseudo-element.
Setting CSS pseudo-class rules from JavaScript
But you can do this instead:
document.styleSheets[0].insertRule("div.button:after { display:'none'; }", 0);
document.styleSheets[0].cssRules[0].style.display= 'none';
Check the link for special IE notes.

How to hide a div in css [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to hide a div with jQuery?
Is there any way to hide the content of div in css without using the display: none;
because it remove the object from code.
I also used the visibility: hidden;
but the image content of div still display
Place the contents inside of a inner div and set that inner div to display none.
Set visibility to hidden and don't set the visibility of the descendants (the images that are showing up) to visible.
with jQuery you can do
$('div').hide();
If you want to be more specific you can use an id or class name instead if div.
ex: $('#myid').hide();
You can store the contents in temporary and then do empty, restore these content to the DIV when you need.
var contents= $('divId').html();
$('divId').html('');

Categories

Resources