I want to do all the following by jQuery and my HTML as well as CSS is below.
- The paragraphs start hidden.
- The paragraphs show and hide when their headings are clicked.
- This process should take 3 seconds.
- Mousing over the paragraph will reverse the colours of the text and background.
- Mousing out will switch the colours back to their original values
jQuery(document).ready(function () {
jQuery('.contentBox').css('display', 'none');//SELECT BY CLASS
$('h2').click(function(){
$(this).next('p').slideToggle();
});
});
body {
background: tan;
}
h1 {
margin-left: 150px;
}
h2 {
margin-bottom: 2px;
padding: 3px 0 2px 25px;
background: #D99E50;
}
p {
margin-top: 0;
padding-top: 0;
}
#mainContain {
background: #523C1E;
width: 80%;
margin: 0 auto;
padding: 30px 0;
}
.contentBox {
background: #524737;
/*Paragraph background color*/
padding: 15px;
color: #FFE5C3;
/*Paragraph font colour*/
}
/*Text Panel Styling*/
.panelContainer {
background: #523C1E;
width: 80%;
margin: 0 auto;
}
<!doctype html>
<html lang="en">
<head>
<!--ADD jQUERY FIRST!-->
<script type="text/javascript" src="jquery-3.6.0.min.js"></script>
<script type="text/javascript" src="lab10-1-jquery.js"></script>
<link rel="stylesheet" href="lab10-1-jquery.css" />
<meta charset="utf-8" />
<title>jQuery FAQ Page</title>
</head>
<body>
<h1 id="headTitle">Frequently Asked Questions</h1>
<div id="mainContain">
<!--MAIN CONTENT CONTAINER-->
<div class="panelContainer">
<!-- 1ST PANEL-->
<h2 id="h21">Do I need a website?</h2>
<p class="contentBox">Morbi hendrerit accumsan orci, tristique aliquam nunc suscipit nec. Vestibulum
consequat ornare nunc, ac tristique urna cursus id. Cras eu viverra leo, vitae rhoncus dui. Vestibulum
rutrum neque ac risus eleifend luctus. Phasellus ultrices, enim quis dignissim tempus, nibh sem faucibus
nisi, at blandit velit massa in nunc. Praesent enim libero, blandit sed massa ut, imperdiet dictum odio.
Maecenas sagittis neque a est porta, at feugiat nulla tempor. Cras vitae tincidunt quam, non interdum
quam. Sed imperdiet lacus nibh, eget viverra eros porta ut. Nulla sed tellus velit. Nullam quam dolor,
malesuada eget bibendum a, lacinia ac ante.</p>
</div>
<div class="panelContainer">
<!-- 2ND PANEL-->
<h2>What is SEO?</h2>
<p class="contentBox">Aliquam felis orci, varius nec facilisis nec, eleifend ac ante. Donec ligula est,
dapibus sed volutpat sit amet, pellentesque id sem. Vestibulum imperdiet volutpat dignissim. Morbi
tincidunt lobortis libero et placerat. Pellentesque dignissim diam laoreet, ultricies ligula aliquam,
consectetur sapien. Praesent luctus orci in vulputate tincidunt. Aenean fermentum ante non elementum
convallis. Sed eget magna enim.</p>
</div>
<div class="panelContainer">
<!-- 3RD PANEL-->
<h2>What is a content management system?</h2>
<p class="contentBox">Morbi hendrerit accumsan orci, tristique aliquam nunc suscipit nec. Vestibulum
consequat ornare nunc, ac tristique urna cursus id. Cras eu viverra leo, vitae rhoncus dui. Vestibulum
rutrum neque ac risus eleifend luctus. Phasellus ultrices, enim quis dignissim tempus, nibh sem faucibus
nisi, at blandit velit massa in nunc. Praesent enim libero, blandit sed massa ut, imperdiet dictum odio.
Maecenas sagittis neque a est porta, at feugiat nulla tempor. Cras vitae tincidunt quam, non interdum
quam. Sed imperdiet lacus nibh, eget viverra eros porta ut. Nulla sed tellus velit. Nullam quam dolor,
malesuada eget bibendum a, lacinia ac ante.</p>
</div>
<div class="panelContainer">
<!-- 4TH PANEL-->
<h2>Do I need a blog?</h2>
<p class="contentBox">Aliquam felis orci, varius nec facilisis nec, eleifend ac ante. Donec ligula est,
dapibus sed volutpat sit amet, pellentesque id sem. Vestibulum imperdiet volutpat dignissim. Morbi
tincidunt lobortis libero et placerat. Pellentesque dignissim diam laoreet, ultricies ligula aliquam,
consectetur sapien. Praesent luctus orci in vulputate tincidunt. Aenean fermentum ante non elementum
convallis. Sed eget magna enim.</p>
</div>
</div>
<!--END mainContain-->
</body>
</html>
6
Answers
To have the slide toggle animation take 3 seconds provide an argument to
slideToggle()
which is the number of milliseconds the animation should take. In this case,3000
, but note that this is very slow. I’d suggest that you shouldn’t make effects last longer than around 500ms.To have the
p
styling change on mouse hover, use the:hover
pseudo selector in your CSS.In addition, it’s better practice to always use CSS to hide content which should not be visible when the page loads. This is in order to avoid a FOUC, which can happen due to JS running after the DOM has fully loaded and been rendered to the screen.
While you seem to have achieved points 1 and 2 already, the easiest way to achieve points 3-5 is as follows, with explanatory code in the comments:
JS Fiddle.
It’s worth noting that a 3000ms animation is slow, and may be perceived as a lack of responsiveness in your site. This may, or may not, be a problem for you but it’s worth testing with your users. My personal inclination is to animate for around 0.4s (400ms), but my use-cases may not reflect your own.
References:
:root
.var()
.slideToggle()
.Here is the solution based on your all requirements:
For better interactivity if you wanted one paragraph to hide on click of another paragraph you can apply a slideUp to every paragraph and then open the required div.
Not sure what is your error. I find an error when I Run code snippet:
If that is your issue, you can check the jquery files in your local or use:
To meet your request, you can try