jQuery Accordion Click Resize

User
.img-acc .accordion ul:hover li {
width: 8%;
}

.img-acc .accordion ul:hover li:hover {
width: 60%;
}
each li has background image now apply above css using jquery on each li click

Assistant
To achieve the functionality of resizing the `li` elements on click using jQuery, you can implement the following script. This script ensures that when an `li` is clicked, it receives a specific width, and the other `li` elements adjust accordingly.

Here’s how to do it:

### HTML Structure (Example)

<div class="img-acc">
  <div class="accordion">
    <ul>
      <li style="background-image: url('image1.jpg');"></li>
      <li style="background-image: url('image2.jpg');"></li>
      <li style="background-image: url('image3.jpg');"></li>
    </ul>
  </div>
</div>

### CSS

.img-acc .accordion ul {
  display: flex;
  margin: 0;
  padding: 0;
  list-style: none;
  height: 200px;
}

.img-acc .accordion ul li {
  flex-grow: 1;
  transition: width 0.3s ease;
  background-size: cover;
  background-position: center;
  cursor: pointer;
}

.img-acc .accordion ul li:hover {
  opacity: 0.8;
}

### jQuery Script

$(document).ready(function () {
  $('.img-acc .accordion ul li').click(function () {
    // Reset all li widths
    $('.img-acc .accordion ul li').css('width', '8%');

    // Set the clicked li width to 60%
    $(this).css('width', '60%');
  });
});

### Explanation
1. **CSS Transition**: The `transition: width 0.3s ease;` in the CSS ensures smooth animation when the width changes.
2. **Default Hover Behavior**: By default, all `li` elements have a width of `8%`, unless overridden.
3. **jQuery on Click**:
– First, reset all `li` elements to their default width of `8%`.
– Then, set the width of the clicked `li` element to `60%`.

This setup ensures that clicking on any `li` resizes it while resetting the others. You can enhance the behavior further if needed, such as restoring sizes when clicking outside the accordion.

User
add left and right arrow and when click arrow apply above widht increase accordingly
Assistant
To include left and right arrows for navigation and dynamically apply the width changes based on the current `li` element, you can enhance the solution as follows:

### Updated HTML Structure

<div class="img-acc">
  <button class="arrow left-arrow">←</button>
  <div class="accordion">
    <ul>
      <li style="background-image: url('image1.jpg');"></li>
      <li style="background-image: url('image2.jpg');"></li>
      <li style="background-image: url('image3.jpg');"></li>
      <li style="background-image: url('image4.jpg');"></li>
    </ul>
  </div>
  <button class="arrow right-arrow">→</button>
</div>

### Updated CSS

.img-acc {
  display: flex;
  align-items: center;
  position: relative;
}

.img-acc .arrow {
  background-color: #000;
  color: #fff;
  border: none;
  padding: 10px 20px;
  cursor: pointer;
  font-size: 16px;
  position: absolute;
  z-index: 10;
  top: 50%;
  transform: translateY(-50%);
}

.img-acc .left-arrow {
  left: 0;
}

.img-acc .right-arrow {
  right: 0;
}

.img-acc .accordion ul {
  display: flex;
  margin: 0;
  padding: 0;
  list-style: none;
  height: 200px;
  width: 100%;
  overflow: hidden;
}

.img-acc .accordion ul li {
  flex-grow: 1;
  transition: width 0.3s ease;
  background-size: cover;
  background-position: center;
  cursor: pointer;
}

### Updated jQuery Script

$(document).ready(function () {
  let currentIndex = 0; // To keep track of the current active li
  const liElements = $('.img-acc .accordion ul li'); // Get all li elements
  const totalItems = liElements.length;

  // Function to update the widths of the li elements
  function updateWidths() {
    liElements.css('width', '8%'); // Reset all widths to default
    liElements.eq(currentIndex).css('width', '60%'); // Increase width of the active li
  }

  // Initial width setup
  updateWidths();

  // Left arrow click event
  $('.left-arrow').click(function () {
    currentIndex = (currentIndex > 0) ? currentIndex - 1 : totalItems - 1; // Move to previous or loop to last
    updateWidths();
  });

  // Right arrow click event
  $('.right-arrow').click(function () {
    currentIndex = (currentIndex < totalItems - 1) ? currentIndex + 1 : 0; // Move to next or loop to first
    updateWidths();
  });

  // Click event on li elements
  liElements.click(function () {
    currentIndex = $(this).index(); // Update the index to the clicked element
    updateWidths();
  });
});

### How It Works
1. **Left and Right Arrows**: Added two buttons (`left-arrow` and `right-arrow`) positioned on either side of the `.accordion`.
2. **Dynamic Index Update**:
– `currentIndex` keeps track of the currently active `li`.
– Clicking the left or right arrow updates this index, either moving to the previous or next element (with wrapping).
3. **Initial Widths**: On page load, the first `li` is expanded.
4. **Reset and Update**: All `li` elements are reset to a width of `8%`, and the active `li` is set to `60%` on arrow or `li` click.

### Output Behavior
– Clicking the left arrow moves focus to the previous `li` and resizes it.
– Clicking the right arrow moves focus to the next `li` and resizes it.
– Clicking directly on an `li` updates its width and adjusts the others.

User
but also keep onclick event
Assistant
To ensure that the `onclick` event on the `li` elements remains functional while incorporating the left and right arrows for navigation, you can enhance the script to handle both the `li` click and arrow navigation seamlessly.

Here's the updated solution:

### Updated jQuery Script

$(document).ready(function () {
  let currentIndex = 0; // To keep track of the current active li
  const liElements = $('.img-acc .accordion ul li'); // Get all li elements
  const totalItems = liElements.length;

  // Function to update the widths of the li elements
  function updateWidths() {
    liElements.css('width', '8%'); // Reset all widths to default
    liElements.eq(currentIndex).css('width', '60%'); // Increase width of the active li
  }

  // Initial width setup
  updateWidths();

  // Left arrow click event
  $('.left-arrow').click(function () {
    currentIndex = (currentIndex > 0) ? currentIndex - 1 : totalItems - 1; // Move to previous or loop to last
    updateWidths();
  });

  // Right arrow click event
  $('.right-arrow').click(function () {
    currentIndex = (currentIndex < totalItems - 1) ? currentIndex + 1 : 0; // Move to next or loop to first
    updateWidths();
  });

  // Click event on li elements
  liElements.click(function () {
    currentIndex = $(this).index(); // Update the index to the clicked element
    updateWidths();

    // Additional onclick functionality
    alert('You clicked on item ' + (currentIndex + 1));
  });
});

### Key Points
1. **Preserve `onclick` Functionality**:
– When an `li` element is clicked, the `currentIndex` is updated, the widths are recalculated, and any additional functionality tied to the click event is executed (e.g., showing an alert).
2. **Arrow and Click Events Work Together**:
– Navigation with the arrows adjusts `currentIndex` and updates the widths.
– Clicking on an `li` directly updates the `currentIndex` and executes any associated logic (like custom behavior or actions).
3. **Custom Behavior in `li` Click**:
– Replace the `alert` inside the `liElements.click()` function with any custom behavior you want to trigger on click.

### Usage
– Clicking the left or right arrow navigates through the `li` elements, resizing them accordingly.
– Clicking directly on an `li` not only resizes it but also triggers any additional `onclick` behavior.

### Output
This script ensures that:
– Arrows work for navigation and resizing.
– Clicking an `li` still executes its `onclick` functionality and resizes it properly.

Leave a Comment

Your email address will not be published. Required fields are marked *