Expanding a div with CSS transition is triggered by a click event in JQuery

Looking to modify the height of the header by expanding and reducing it when clicking on the "arrow" div. Attempted to use addClass with jQuery without success. HTML:

<header>
    <p>The title..</p>
    <h1>Some text</h1>
      <div class="arrow" id="arrow">
        <img src="img/arrow.svg" />
      </div>
</header>

CSS

header { 
  background: #2282A4;
  color: #fff;
  text-align: center;
  width: 100%;
  height: 450px;
  transition: height 0.5 ease;
}
expandheight {
  height: 850px;
}

JQuery:

$(document).ready(function() {
   $(function() {
      $('#arrow').click(function() {
         $('header').addClass('expandheight');
}); 
}); 
});

In need of assistance in decreasing the height with the same button, removing the "expandheight" class when active and adding it when not... Struggled with if/else statements.

Answer №1

You are encountering several syntax errors:

  1. expandheight should be styled using .expandheight
  2. Ensure cross-browser compatibility for animation properties
  3. Implement the use of toggleClass to easily add or remove classes

$(document).ready(function() {
  $(function() {
    $('#arrow').click(function() {
      $('header').toggleClass('expandheight');
    });
  });
});
header {
  background: #2282A4;
  color: #fff;
  text-align: center;
  width: 100%;
  height: 450px;      
  -webkit-transition:height 0.5s ease;
  -moz-transition:height 0.5s ease;
  transition:height 0.5s ease;
}

.expandheight {
  height: 850px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<header>
  <p>The title..</p>
  <h1>Some text</h1>
  <div class="arrow" id="arrow">
     <img src="img/arrow.svg" />
  </div>
</header>

Answer №2

To expand or decrease the height of the header, it is recommended to use toggleClass() instead of addClass()

$(document).ready(function() {
    $(function() {
        $('#arrow').click(function() {
            $('header').toggleClass('expandheight');
        });
    });
});

There were some errors in your previous code. I have created a jsfiddle link to demonstrate it working correctly.

https://jsfiddle.net/7yodz723/

(I have added a border around the arrow for better visualization of this example)

Answer №3

To easily switch between different classes, you can simply use the toggleClass method.

$(document).ready(function() {
  $(function() {
    $('#toggleButton').click(function() {
      $('section').toggleClass('expandwidth');
  });
 });
});
header { 
  background: #444;
  color: #fff;
  text-align: center;
  width: 100%;
  height: 300px;
  transition: width 0.5s ease;
}
.expandwidth {
  width: 80%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<section>
  <p>Some content..</p>
  <h2>More text here</h2>
  <button id="toggleButton">Toggle</button>
</section>

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

Tips for concealing a div element once the final section is reached

My website has a sticky footer with a clickable arrow that allows users to navigate through the sections. However, I am facing an issue where the arrow does not disappear once the last section is reached. Being new to jQuery and JS, I'm unsure how to ...

Can you explain the distinction between data-ng and ng?

I recently came across a discussion about the distinction between ng-* and data-ng-* in AngularJS. Apparently, using data-ng-* ensures that the HTML remains valid. But I can't help but wonder, why is this necessary when I can simply declare the ang ...

A method for increasing a counter using only an instance of a class or function without accessing its methods or properties in Javascript

Looking at the task ahead, let increment = new Increment(); I have been tasked with creating a Javascript class or function called Increment in order to achieve the following: console.log(`${increment}`) // should output 1 console.log(`${increment}`); ...

Exploring the Benefits of Using JSON in AJAX Requests

Can you help me determine the best way to extract data from a php page using ajax in the form of a json array? Here is an example code snippet: $.ajax({ type: "get", url: "assets/update_cart_user_fstore.php", data: up, cache: false, su ...

Verify the type of email domain (personal or corporate)

Here's an example: isPersonalEmail("<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0aea1ada580a7ada1a9aceea3afad">[email protected]</a>") // true isPersonalEmail("<a href="/cdn-cgi/l/email- ...

Ways to enable automatic scrolling of a page as the content expands (Utilizing collapsible elements)

For a recent project I've been working on, I decided to include 4 collapsible text boxes. However, I encountered an issue where opening content1 would expand and push down content2-4, requiring the user to manually scroll to view the remaining collaps ...

The Justin TV API: Leveraging the Power of Jquery and JSON

Could someone assist me with using the Justin TV API? I have looked through their documentation and found an example along with returned values (Here's the xml file). However, I am having trouble understanding where it specifies which channel to pull ...

What is the limit of CSS includes that IE can process?

While working on theming Drupal, I encountered an unusual issue. After enabling a few modules that added 5 to 10 link tags to the page, I noticed a strange behavior in different browsers. Firefox successfully added the new stylesheets to the cascade, but i ...

Encounter a syntax error in Google Maps JavaScript API v3 while accessing it on BlackBerry 7

Currently, I am developing a worklight hybrid application that utilizes the Google Maps v3 JavaScript API to load maps. However, I have encountered an issue when attempting to load maps using jQuery AJAX - specifically, I am receiving a syntax error: parse ...

Pause the initial ajax data loading in Tabulator.js and instead load data only after the first filter is applied

Currently, I am utilizing the tabulator.js library and hoping to delay the loading of data until after the first filter is applied, rather than immediately upon page load. Is there a way to achieve this functionality using something like: initialAjaxLoa ...

fetch Active Directory user details using React

I'm working on a React web application. I need to pull user information from Active Directory and verify if the user belongs to a specific AD group. Is this achievable? What would be the best approach to create an API for this integration? ...

When removing a specific option from a dropdown menu, the default selection is chosen instead of the

So I have a select element that initially had multiple options selected. After I manipulated it with my code, only one option remains selected, but because I removed the previous selections, the default option is now being shown instead of the next selecte ...

Having trouble retrieving data from the controller in JSON format

I am having trouble getting the data from the controller to the view in JSON format, and I can't figure out why it's not working. This is my controller (I request it on the index action): public class HomeController : Controller { ...

Cross-domain scripting

I have a unique javascript code hosted on my server. I want to offer website visitors a similar implementation approach to Google Analytics where they can easily embed the script on their servers. For instance: <script type="text/javascript" src="http: ...

ALSO, various criteria

function findLogicalAND(){ let result; let index; for (index = 0; index < arguments.length; index++){ result = arguments[index] && arguments[index+1]; } return result; } console.log(findLogicalAND(true, true, false, false)); I want to r ...

Every time I try to deploy my app on Heroku, it keeps crashing with a strange error popping up on the home screen

Here are the logs for my application: 2018-07-19T01:40:27.548845+00:00 app[web.1]: at Object.<anonymous> (/app/node_modules/bcrypt/bcrypt.js:6:16) 2018-07-19T01:40:27.548847+00:00 app[web.1]: at Module._compile (module.js:652:30) 2018-07-19T01:40:27 ...

Rendering Based on Conditions in React Native

I'm a beginner in the world of React Native and coding and I'm looking to display text based on certain variables (as shown below). If isPlayer === true && base.length === 1, then display x Else if isPlayer === true && base.leng ...

Exploring the functionalities of command line arguments in Vue.js

Is there a way to set command line arguments for a Vue.js application? I am utilizing Vue 3 on the frontend which interacts with a Python backend (Flask) using Axios. Currently, I am defining the baseURL for Axios in my main.js file as follows: import axio ...

Ways to determine if an object contains an array

Looking at the following data structure: [ 13, { a: [ [ '2988.30000', '0.19000000', '1549294216.653040' ] ] }, { b: [ [ '2988.30000', '0.00000000', '1549294216.653774&a ...

Trouble with saving data to a database using AJAX

I've been struggling with a function that involves ajax. No matter how much I try, I just can't seem to pinpoint the issue. The goal is to trigger an alert when a user tries to add a duplicate entry to the database by selecting a checkbox. <s ...