Shift the position of an element horizontally when another element is being hovered over

Attempting to create a unique swiper slider using CSS, here is what has been accomplished so far in terms of HTML:

 <section id="lakeSlider" class="carousel vcenter-item py-5">
            // More HTML code here
    </section>

CSS Styles:

/* lakeSlider styles */

#lakeSlider .carousel {
    width: 100%;
    padding: 30px;
    // Additional styling properties
}

// More CSS classes and rules here
    

View image here. Current behavior described as follows: Hovering on any card causes elements to move. Desired behavior outlined below.

Desired Behavior:

  • Hovering on first two elements moves right elements to the right 100px. *Middle element hover results in left elements moving left 100px, right elements moving right 100px. *Hovering on last two elements shifts left elements to the left 100px. Thank you for your assistance and patience.

An attempt was made to customize the CSS by altering this line:

@media (min-width:960px) {
    #lakeSlider .carousel-item:hover~.carousel-item {
        -webkit-transform: translate3d(100px, 0, 0);
        transform: translate3d(100px, 0, 0);
    }

Adjustment made but did not yield desired result. Need further guidance to achieve intended functionality.

Answer №1

Most major browsers now support the CSS :has pseudo-class, with Firefox being an exception that requires users to set a flag.

This code snippet utilizes the :has pseudo-class on the container element to detect when one of the items is hovered over. When this happens, it shifts all items by -100px (noting that the transform-origin remains at the default center position for all items).

Furthermore, it applies a transformation specifically for the items to the right of the hovered item, translating them by 100px. As for the hovered item itself, it remains in place with no translation.

<!DOCTYPE html>
<html>
<head>
<style>
/* lakeSlider */

#lakeSlider .carousel {
    width: 100%;
    padding: 30px;
    padding-top: 80px;
    position: relative;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}

#lakeSlider .carousel__container {
    margin: 70px 0px;
    padding-bottom: 10px;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
}

#lakeSlider .categories__title {
    color: rgb(77, 55, 102);
    font-size: 28px;
    position: absolute;
    padding-left: 30px;
}

#lakeSlider .carousel-item {
    width: 280px;
    margin: 50px 5px;
    overflow: hidden;
    display: inline-block;
    cursor: pointer;
    -webkit-transition: 1000ms all;
    transition: 1000ms all;
}

@media (min-width:960px) {
#lakeSlider .carousel__container:has(.carousel-item:hover) .carousel-item {
  transform: translateX(-100px);
  opacity 0.3;
}
#lakeSlider .carousel__container .carousel-item:hover ~ .carousel-item {
  transform: translateX(100px);
}
#lakeSlider .carousel__container:has(.carousel-item:hover) .carousel-item:hover {
  transform: translateX(0) scale(1.3);
  opacity: 1;
}
}

@media (max-width:960px) {
    #lakeSlider .carousel__container:hover .carousel-item:hover {
        opacity: 1;
    }
}

#lakeSlider .card-body {
    background-color: #2699FB;
    color: white;
}

#lakeSlider .card-body h5 {
    font-family: 'Poppins', sans-serif;
    font-weight: 400;
}

#lakeSlider .card-body p {
    font-family: 'Poppins', sans-serif;
    font-size: 14px;
    font-weight: 100;
}
</style>
</head>
<body>
 <section id="lakeSlider" class="carousel vcenter-item py-5">
        <div>
            <div class="carousel__container">
                <div class="carousel-item carousel-index1">
                    <div class="card">
                        <img class="carousel-item__img img-fluid"
                            src="./assets/images/cards/oserenfloatingcardimageindex2.png" alt="" />
                        <div class="card-body pb-0">
                            <h5 class="text-center card-title">Mila Hubert</h5>
                            <p class="card-text text-center">Sales Manager</p>
                        </div>
                    </div>
                </div>
                <div class="carousel-item carousel-index2">
                    <div class="card">
                        <img class="carousel-item__img img-fluid"
                            src="./assets/images/cards/oserenfloatingcardimageindex2.png" alt="" />
                        <div class="card-body pb-0">
                            <h5 class="text-center card-title">Boris Komnenov</h5>
                            <p class="card-text text-center">Marketing Manager</p>
                        </div>
                    </div>
                </div>
                <div class="carousel-item carousel-index3">
                    <div class="card">
                        <img class="carousel-item__img img-fluid"
                            src="./assets/images/cards/oserenfloatingcardimage.png" alt="" />
                        <div class="card-body pb-0">
                            <h5 class="text-center card-title">Joshua Rubens</h5>
                            <p class="card-text text-center">CEO</p>
                            <div class="cardbodydownarrow text-center m-0 p-0"><img
                                    src="./assets/images/icon/oserendownarrowpix.png" alt="">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="carousel-item carousel-index4">
                    <div class="card">
                        <img class="carousel-item__img img-fluid"
                            src="./assets/images/cards/oserenfloatingcardimageindex2.png" alt="" />
                        <div class="card-body pb-0">
                            <h5 class="text-center card-title">Anna Jimenez</h5>
                            <p class="card-text text-center">Product Designer</p>
                        </div>
                    </div>
                </div>
                <div class="carousel-item carousel-index5">
                    <div class="card">
                        <img class="carousel-item__img img-fluid"
                            src="./assets/images/cards/oserenfloatingcardimageindex2.png" alt="" />
                        <div class="card-body pb-0">
                            <h5 class="text-center card-title">Ben Hubert</h5>
                            <p class="card-text text-center">Programmer</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
    </body>
    </html>

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

Issue with jQuery selector in Internet Explorer, but functions correctly in Firefox

Why is it that this code functions in FF but not IE? Any insights would be appreciated. The issue appears to lie with $(".ui-tabs-nav li a"). In IE, this.text returns undefined. Check out the code here. ...

Scrolling vertically using window.open functionality

I am trying to open a pop-up window using window.open. I would like the scrollbars to appear only if necessary. Unfortunately, in Safari, the scrollbars do not show up unless I include scrollbars=1 in the code. However, this also causes horizontal scrollb ...

The live() function is causing issues with my ajax request

Within my webpage, I have a link with an onclick() event that should display a div containing a date input text named "datepicker0", followed by another div with the id of "bContent". I've included the script below to implement a date filter on the d ...

What is the best way to use jQuery to filter data in a table by clicking on a specific table row?

My website contains a table with player names and servers, but I want to make them clickable for filtering purposes. For instance, clicking on a player name should reload the leaderboards to show which servers that player plays on, and clicking on a server ...

Leveraging JQuery to extract the numerical value located between the slashes within a hyperlink

I need to extract numeric values from a link like this. For example: /produkt/114664/bergans-of-norway-airojohka-jakke-herre In this case, I want to fetch 114664. To achieve this, I have written the following jQuery code: jQuery(document).ready(functi ...

Issue with JS jQuery: The click event on an li element within an expanded ul does not function properly

I have built a basic webpage to explore jQuery. However, there is an issue that arises when I click on the "Button: another one." It seems to interfere with the event of clicking on the li element, causing it to glitch and not respond. Here is the code: JS ...

Where can I locate the Subresource Integrity (SRI) code specifically for the debug build of Bootstrap 5?

If you're looking for Bootstrap 5.3.0-alpha3, you can find it hosted on the JsDelivr network. Check out this link While it's easy to find the SRI code for the minified version by right-clicking the copy icon, where can you locate the SRI code fo ...

Can using the jQuery.clone method impact other functions?

Assuming $dom is a jQuery element, can the following line be safely removed if a is not utilized thereafter? var a = $dom.clone(true,true); When using $dom.clone(false,false), it appears that there are no side effects. I believe this method doesn't ...

Adjusting HTML image sizes

How can I prevent large images from extending off the edge of the screen when using HTML img tags? Is there a way to scale them to fit within the browser window or specify a set percentage for their width and height? I've noticed that using the width ...

How can I use JQuery to sum up the numbers within div elements?

Is there a way to automatically number the generated blocks? For example, the first block would display "1", the second "2" and so on. Below is my current code. Simply input the desired number of blocks in the input field. <!DOCTYPE html> <html ...

Implement the parent jQuery library within a child iframe

I have a query regarding the use of two iframes in my HTML page. I am trying to implement a single jQuery file that is loaded on the parent page. The code snippet provided below works perfectly on all browsers, except for one issue with Chrome when using t ...

Customizing the colors of an Ant Design header

I am currently attempting to modify the background color of the entire header in antdesign. I believe the file containing the default settings can be found here: https://github.com/ant-design/ant-design/blob/master/components/style/themes/default.less Upo ...

Adding multiple variables in jQuery: A guide to mimicking the .= operator from PHP

Being new to jQuery, I'm a bit unsure of how to achieve this. Typically in php, I can accomplish something like this: $result = ''; $result .= 'Hi'; $result .= ' there'; echo $result; I'm simply wondering if there ...

Items added to a form using jQuery will not be submitted when the form is posted

Issues with posting data from dynamically appended options in a form select using jQuery have been noticed. When an appended option is selected and the form is submitted, the value does not get posted (in this case, in an email template). Adding another no ...

Using the XMLHttpRequest object for AJAX file uploads that are compatible with Internet Explorer 9

Seeking help with uploading a file using ajax. The current code works on all browsers except for i.e 9 and older versions. I need to find a solution that will also work on i.e. Some suggestions have been to use an iframe, but I don't see how that res ...

Using jQuery, generate a dynamic form to create a multidimensional array

I've set up a form where additional dropdowns can be dynamically added if the user clicks on a specific link. Here's an example of how it looks: <div class="dynamic-sale"> <select name="sizes[]" id="sizes" class="entry-dropdown"&g ...

Automatically populate form fields with data from database using AJAX when a value is present

One challenge I have is auto-filling a form using Ajax and PHP, focusing on a unique field like a mobile number. The idea is that if the mobile number already exists in the database, then the rest of the form fields should be filled with the corresponding ...

What is the best way to retrieve AJAX data in Django views?

I am currently facing difficulties with retrieving data from an AJAX call in my Django views using the GET method. Unfortunately, I am encountering issues accessing the data within my views and the console.log statements are not showing any output. As a ne ...

Loading External Content with Jquery Ajax Made Easy

Today marks the beginning of my coding journey with jQuery. I believe that practice is key to learning effectively. My goal is to remove existing content from #container and replace it with new content from an external page. Sounds simple, right? I'v ...

Utilizing transitions to control the visibility of div elements

This is what I have achieved so far: function getOffsetRect(elem) { var box = elem.getBoundingClientRect() var body = document.body var docElem = document.documentElement var scrollTop = window.pageYOffset || docElem.scrollTop || body.sc ...