Interactive jQuery element featuring an image gallery displayed within a translucent black overlay on a webpage

My goal is to add a jQuery popup with an image that appears automatically when a website loads. I envision the popup creating a black transparent overlay, similar to this example. While setting up the basic popup was straightforward, integrating the image has proven to be a challenge. Ideally, I want the popup to be positioned 30% from the left border of the page. Furthermore, both the image and the black overlay should disappear on hover and reappear when the user moves the mouse. How can I achieve this?

Below is the HTML code snippet:

<head>
    script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
 </head>
 <body>
    <a class="cha" data-popup-open="popup-imonelle" href="#"></a>
    <div class="cha-popup" data-popup="popup-imonelle">
       <div class="popup-pic">
          <img src="Images/imonelle.jpg"/>
          <a class="popup-hover" data-popup-close="popup-imonelle" href="#">x</a>
       </div>
    </div>
 </body>
 <footer>
    <script src="js/Scriptquery.js"></script>
 </footer>

Here is the associated jQuery code:

$(function() {
    //----- OPEN on page load
    $(function(e)  {
        var targeted_popup_class = $('[cha-popup-close]').attr('cha-popup-close');
        $('[cha-popup="' + targeted_popup_class + '"]').fadeOut(350);
    });

    //----- CLOSE on hover
    $('[cha-popup-close]').on('hover', function(e)  {
        var targeted_popup_class = jQuery(this).attr('cha-popup-close');
        $('[data-popup="' + targeted_popup_class + '"]').fadeOut(350);

        e.preventDefault();
    });
});

Lastly, the CSS styles for the popup:

.cha-popup {
    width:100%;
    height:100%;
    display:none;
    position:fixed;
    top:0px;
    left:0px;
    background:rgba(0,0,0,0.75);
}

/* Inner */
.popup-pic {
    max-width:845px;
    width:90%;
    padding:0px;
    position:absolute;
    top:50%;
    left:30%;
    -webkit-transform:translate(-50%, -50%);
    transform:translate(-50%, -50%);
    box-shadow:0px 2px 6px rgba(0,0,0,1);
    border-radius:3px;
    background:#fff;
}

/* Close on hover functionality */
.popup-hover {
    transition:ease 0.25s all;
    -webkit-transform:translate(50%, -50%);
    transform:translate(50%, -50%);
}

I have attempted to adapt my existing popup code for the image integration described above, but it seems like something is amiss. Any guidance would be greatly appreciated.

Answer №1

One of the main issues here lies in the selectors being used, along with some minor syntax problems. Let's delve into these problems and see how they can be resolved...

    //[popup-close-btn]
    var targeted_popup_class = $('[popup-close-btn]').attr('popup-close-btn');

    //[popup]
    $('[popup="' + targeted_popup_class + '"]').fadeOut(350);

    //[data-popup]
    $('[data-popup="' + targeted_popup_class + '"]').fadeOut(350);

The square bracket selector like [popup-close-btn] is specifically searching for an attribute named popup-close-btn.

Upon examining your HTML code, it appears that there are no elements with attributes named popup or popup-close-btn, rendering the selectors ineffective.

Only the [data-popup] selector will yield results since there are indeed elements with a data-popup attribute present.


To address this issue effectively, consider utilizing the following corrected script:

$(document).ready(function() {

    //Find popup to display and fade it in
    var targeted_popup_class = $('[data-popup-open]').data('popup-open');
    $('[data-popup="' + targeted_popup_class + '"]').fadeIn(350);

    //Close on hover
    $('[data-popup-close]').on('mouseover', function(e) {

        //Locate popup to close and hide it
        var targeted_popup_class = $(this).data("popup-close");
        $('[data-popup="' + targeted_popup_class + '"]').fadeOut(350);

    });

});

Take a look at the corrected example here.

In addition to rectifying the selectors, I've cleaned up the code structure, removed unnecessary curly brackets, and notably replaced .on("hover") with .on("mouseover") for improved functionality.

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 method to assign an initial value to React.createRef()?

Scrolling management in my component is handled through a ref that represents the current scroll value. I've chosen to use refs instead of state and setState because using setState while the user is scrolling would result in a choppy and unresponsive ...

Contrast between the expressions '$(<%= DDL.ID %>) and $('<%= DDL.ID %>')

I spent hours trying to attach an event to a drop-down list with no success. I even sought help in a JavaScript chat room, but couldn't find a solution. However, by randomly attempting the following code: $('<%= ddl.ID %>').bind(&apos ...

Selenium EventFiringWebDriver JavaScript: There is a SyntaxError due to a missing closing parenthesis after an argument in

Attempting to run a javaScript command through the EventFiringWebDriver class in Selenium. EventFiringWebDriver eventFiringWebDriver = new EventFiringWebDriver(driver); eventFiringWebDriver.executeScript("document.querySelector('[ng-reflect-title=&ap ...

How can I access the most up-to-date state value in an event listener within a React element?

Here is the code I am working with: var C = () => { var [s, setS] = useState(0) var dRef = useRef<HTMLDivElement>() useMount(() => { setShortcut("ArrowDown", () => { setS(s+1) }) }) return ...

Guide for using JavaScript to obtain the current line position within a contenteditable div during a keypress event

I am in the process of developing a customized editor using a contenteditable div. I require a javascript code that can determine the line position of the current caret position during a keypress event. It should be able to adapt when new lines are added o ...

Node.Js made user authentication effortless

Struggling to integrate user authentication using Passport, Express, and Node.Js as tutorials mostly focus on MongoDB. However, I prefer Neo4J for my database. The examples on passport-local don't fit my needs since I've already implemented a loc ...

The color of my Navbar remains stationary despite implementing Javascript for scroll effects

I am having trouble with the Javascript portion of my code, which I copied from a tutorial. The Navbar is not changing color when scrolled, and I'm not sure if the issue lies with the Javascript, the CSS, or if I need to add 'scrolled' to th ...

Ensure that the Nivo Slider height is set to the accurate final value before the images finish loading

I've integrated Nivo Slider into my responsive website to display an image gallery. The slider spans 100% width and adjusts its height based on the aspect ratio of the images (all images have the same height). However, if the user's connection i ...

Creating an interactive Google line chart in MVC4

I am currently working on a dynamic line chart that needs to be able to adjust the number of lines (Standard, Latest, Earliest, Average) based on the database records. My code structure is similar to this example. function drawChart() { var data = ...

The react-redux developer tool appears to be disabled and is not displaying the current state of the application on the extension toolbar

Just finished the redux-tutorial and attempting to view the state in the redux-devtools. However, the redux-devtools seems to be inactive on the extensions bar. Upon clicking it, a menu appears with options like "to right, to left etc". Selecting one of ...

Show only future events using JavaScript and XML

I am facing a challenge with an interactive map that showcases event dates in various regions of the United States. While the map successfully extracts data from an XML document and displays it, I am encountering an issue where events from 2014 are not bei ...

Utilizing a dropdown list in HTML to dynamically change images

On my HTML page, I have implemented a dropdown list box. My objective is to change an image and update a label based on the selection made from the dropdown list box. I already have an array called 'temp' which represents the number of items. The ...

AngularJS: Calculate the total sum of array elements for generating charts

When using tc-angular-chartjs, I have successfully implemented a pie chart but am struggling to sum the label values for proper display. Additionally, while I can create a bar chart using their example, I encounter issues when trying to use external data f ...

Display the text above overlapping div elements

I need a design where one div overlaps another, but the text inside the overlapping div must still be visible. <div class='box1'> <div class='sendAbove'> This message should remain visible in this div </div> ...

Switching the navbar image with HTML and JavaScript when clicked

Looking to change the image upon clicking on any of the navbar items. Emulating the navigation bar behavior from this website : This is my current progress : The HTML file : <html lang="en"> <head> <meta charset="utf-8" /> ...

Having difficulty with sorting an array of objects

I am currently facing an issue in React where I have an array of objects with a createdAt attribute that I need to sort. Despite my efforts, the sorting doesn't seem to be working correctly as the output is not in the expected order. I have included t ...

Exploring how to replicate background-size: cover with a center position using HTML5's <video> tag

Looking to replicate the effect of background-size: cover on a <video> element. I was able to achieve this with the code below: <video id="videobackground" autoplay="true" loop="true"> <source src="BackgroundFinal2.mp4" type="video/mp4" ...

Transfer files and data efficiently using Ajax

I am facing an issue while trying to send a file and data together to the Controller. When I only send the file, it works correctly. However, when attempting to send both data and a file, nothing seems to work. contentType: false, processData: false, Rem ...

There seems to be a disconnect between the React Redux store

When attempting to connect my store to a React application, I encountered the following error: TypeError: state is undefined store/index.js (Creating Reducer function) import {createStore} from 'redux'; const counterReducer = (state:{counter:0} ...

Leveraging Vuetify on a standalone .html document

I am currently working on a personal project where I am experimenting with avoiding a full npm build process. Instead, I am trying to work within a single .html file that utilizes Vue 3 and Vuetify. Below is the complete HTML code which can be simply dropp ...