Struggling with adding or removing classes from the footer and icon

I'm attempting to create a dynamic footer that expands to reveal additional information when clicked, and then contracts back to its original state upon clicking a specific icon. The goal is to toggle between displaying time information and hiding it.

Even after trying to utilize jquery for altering the CSS properties and adding/removing classes, I haven't been able to achieve the desired functionality.

<div class="footer" onclick="showTimes()">
  <i class="fas fa-times-circle hidden" onclick="hideTimes()"></i>
  <div id="timeDiv" class="time"></div>
</div>
function showTimes(){
  $(".footer").css("height", "40%");
  $("#timeDiv").addClass( "hidden" );
  $(".fa-times-circle").removeClass( "hidden" );
}

function hideTimes(){
  $("#timeDiv").removeClass( "hidden" );
  $(".fa-times-circle").addClass( "hidden" );
  $(".footer").animate({height:'10%'},400);
}
.footer {
  background-color: #211f20;
  width: 70%;
  height: 10%;
  right: 10px;
  position: fixed;
  bottom: 0px;
  z-index: 1;
  opacity: 0.7;
  cursor: pointer;
}

.footer:hover {
  height: 12.5%; !important;
  webkit-transition:height 0.5s; /* Safari */ !important;
  transition:height 0.5s; !important;
}

.time {
  color: white;
  font-family: 'Montserrat', sans-serif;
  font-size: 2vh;
  position: absolute;
  bottom: 20%;
  width: 100%;
  text-align: center;
  display: inline-block;
}

.fa-times-circle {
  color: white;
  font-size: 5vh;
  position: absolute;
  top: 0;
  right: 0;
  margin: 20px 20px 0px 0px;
}

My expectation was for the button to disappear upon clicking, and for the "timeDiv" to become visible again with the footer reverting to its original size.

Answer №1

It appears there are several reasons why this functionality is not working as expected.

  • .footer:hover might need to be changed to .footer:active
  • The CSS properties mentioned above have double semi-colons - both before and after the !important declaration in each line
  • showTimes() seems to always trigger, even when clicking on the icon meant to execute hideTimes()

Additionally, it could be beneficial to enclose the icon and #timeDiv within a container div.

Below is my proposed solution:

The main points to note include:

  • Using stopPropagation() in the hideTimes() function
  • Fixing the semi-colon issue in the CSS code
  • Introducing a wrapper div, updating the reference from the icon to the wrapper for the .hidden class, and adjusting each function to show/hide the class on the wrapper accordingly.

function showTimes(){
  $(".footer").css("height", "40%");
  $(".time__container").removeClass( "hidden" );
}

function hideTimes(event) {;
    event.stopPropagation();
    
  $(".time__container").addClass( "hidden" );
  $(".footer").animate({height:'10%'},400);
}
/* Added `.hidden` class to simulate your 3rd party css */
.hidden {
    display: none;
}

.footer {
  background-color: #211f20;
  width: 70%;
  height: 10%;
  right: 10px;
  position: fixed;
  bottom: 0px;
  z-index: 1;
  opacity: 0.7;
  cursor: pointer;
}

.footer:active {
  height: 12.5% !important;
  webkit-transition:height 0.5s /* Safari */ !important;
  transition:height 0.5s !important;
}

.time {
  color: white;
  font-family: 'Montserrat', sans-serif;
  font-size: 2vh;
  position: absolute;
  bottom: 20%;
  width: 100%;
  text-align: center;
  display: inline-block;
}

.fa-times-circle {
  color: white;
  font-size: 5vh;
  position: absolute;
  top: 0;
  right: 0;
  margin: 20px 20px 0px 0px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script src="https://code.jquery.com/jquery-3.1.1.js"></script>
</head>
<body>
<div class="footer" onclick="showTimes()">
    <div class="time__container hidden">
                <i class="fas fa-times-circle" onclick="hideTimes(event)">X</i>
                <div id="timeDiv" class="time">lorem ipsum</div>
            </div>
        </div>
</body>
</html>

Answer №2

$(".footer").on("click",function(){
  $(".footer").css("height", "40%");
  $("#timeDiv").addClass( "hidden" );
  $(".fa-times-circle").removeClass( "hidden" );
})

$(".fa-times-circle").on("click",function(){
 $("#timeDiv").removeClass( "hidden" );
  $(".fa-times-circle").addClass( "hidden" );
  $(".footer").animate({height:'10%'},400);
})
.footer {
  background-color: #211f20;
  width: 70%;
  height: 10%;
  right: 10px;
  position: fixed;
  bottom: 0px;
  z-index: 1;
  opacity: 0.7;
  cursor: pointer;
}

.time {
  background: pink;
  font-family: 'Montserrat', sans-serif;
  height: 100%;
  width: 20%;
  position: absolute;
  top: 0%;
  left: 50%;
  text-align: center;
  display: inline-block;
}

.time.hidden {
  display: none;
}

.fa-times-circle {
    background: #8BC34A;
    font-size: 5vh;
    position: absolute;
    top: 0px;
    right: 0px;
    margin: 20px 20px 0px 0px;
    z-index: 99999999999;
    width: 30px;
    height: 30px;
}

.fa-times-circle.hidden {
   display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <div class="footer">
    <i class="fas fa-times-circle hidden"></i>
    <div id="timeDiv" class="time">My time</div>
  </div>

Does this meet your requirements?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the functionality of an AngularJS Module?

I am curious about the inner workings of AngularJS modules After experimenting with AngularJS, I came across some puzzling observations. Including AngularJS without registering a module Upon including AngularJS without registering a module, I encountere ...

Utilizing vue-i18n for translating the default value of a Vue component's property

Looking for a solution to localize the default value of a component prop using vue-i18n. Here's an example: export default { name: 'ExampleComponent', props: [{ prompt: { required: false, type: String, default: $t(& ...

Showcasing all products via the terminal using the Mailchimp API in JavaScript

I am currently utilizing mailchimp to showcase all the email addresses from an audience. To achieve this, I have implemented the following code: const client = require("@mailchimp/mailchimp_marketing"); client.setConfig({ apiKey: "MY API KE ...

Extract information from an array located inside a nested object

My aim is to calculate the length of the skills array for each user individually. To begin, I have this JSON data: const txt = `{ "Alex": { "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" da ...

Issue: EMFILE - exceeding maximum number of opened files

I'm currently working with nw.js, attempting to save images in an array of img tags with random names. However, I've encountered a few errors and I'm unsure what's causing them. for (i = 0; i < imgs.length; i++) { request(imgs[i].g ...

Is there a way to retrieve the timestamp of a DOM change event when using MutationObserver for event tracking?

Currently, I am successfully using MutationObserver to monitor changes in the DOM. However, I would like to include a timestamp for each event. Unfortunately, there doesn't seem to be a timestamp property available in the MutationRecord. https://deve ...

Modal visibility glitch: Bootstrap concealing one modal while erroneously revealing another

Upon loading, a button named .giveawayApplyNow is present in a modal. When clicked, the following code is triggered: $('.giveawayApplyNow').click(function() { $('#GiveawayModal').modal('hide'); $('#GiveawayModal&a ...

CSS/HTML leading to incorrect paragraph rendering

One thing I'm struggling with on my website cur-mudg-eon.com is getting a paragraph to display correctly. When you land on the main page, you'll see that "Paragraph #1" appears to the right of H2 "[ker-muhj-uhn]" instead of aligning left below th ...

How to perfectly align an image within a button using CSS

I need help creating a button with an image using the following code: .btnWo { position: relative; outline: none; border: none; height: 150px; width: 400px; border-radius: 10px; color: #FFF; } .btnWo img { height: 150px; ...

Make the jQuery toggle() function work like a regular radio button when selecting multiple options at a time

I have recently created two radio buttons using <i> font icons. Previously, I had successfully used the same code to create a checkbox, so I applied it to the radio buttons as well. After fixing the positioning, everything seemed fine when interactin ...

Unable to retrieve data from the array

I am encountering an issue while trying to fetch data from an array, as I keep receiving undefined Please refer to the image for a visual representation of my problem. I'm not sure what I might be overlooking, so any help would be greatly appreciate ...

The datetimepicker by Trentrichardson is failing to function

I've been attempting to utilize the timepicker mentioned here. Unfortunately, I seem to be encountering some issues with getting it to work properly. Following the instructions provided by the author, I have included all the necessary files in the cor ...

Having trouble bringing my custom-built Angular module into my Angular application

Currently considering the utilization of this Yeoman generator as a starting point for a small project that will contain several reusable form components to be published. The generator constructs a module and an example component, directive, pipe, and serv ...

Unable to locate the 'react-native' command, attempted various fixes but none were successful

Working on an older react native project that was functioning perfectly until I tried to pick it back up and encountered a problem. https://i.stack.imgur.com/1JUdh.png This issue revolves around the package.json file. https://i.stack.imgur.com/v6ZEf.png ...

Refresh the copyright year by syncing with a time server

Not long ago, I found myself needing to update the copyright year on a website. Instead of using JavaScript to get the year from the system time, I began wondering if there was a way to utilize an internet time server for this purpose. I believe that util ...

Automatically clear out table rows once the maximum row limit is reached, then append new data using the WebSocket data in JavaScript

Recently, I delved into JavaScript and faced a dilemma with my Bitcoin Price update dashboard. The data is streaming in through an external WebSocket, updating the prices every second. But here's the catch - the table rows are going crazy! To maintain ...

The CSS styling feature is not functional within the JavaMail API

After receiving an email sent using the JavaMail API, I noticed that the CSS styles are not being applied and only the HTML content is rendered. HTML CODE <!doctype html> <html lang="en"> <head> <meta charset="u ...

Dynamic, AJAX-driven content is experiencing a 200 status code

Is it necessary for a single-page website, which dynamically loads content via ajax and utilizes browser session history stacking, to return a "200 Status" for each successful state change? The core code of my website is as follows: $(document).on('c ...

What is the correct way to type this?

Looking for some help with my React Component: const CustomIcon = ({ icon, ...props }: { icon: keyof IconType }) => { const Icon = Icons[icon] return <Icon {...props} /> } I'm attempting to use it like this: <CustomIcon icon={normalTe ...

access pictures from a different directory using JavaScript

I am working with an images array that contains three jpg files. I am trying to set the background image of a class called pic using images from the array. The issue I'm facing is that the images are stored in an images folder with the URL images/. I ...