Adjust the placement of a fresh element when hovering over another element

I am looking to display a small menu (arrow_box) when the user hovers over an element.

<div class="wrap">
    <div class="element"></div>
    <div class="element"></div>
    <div class="element"></div>
</div>

<div class="arrow_box"></div>

.arrow_box {
    position: relative;
    background: #88b7d5;
    border: 4px solid #c2e1f5;
}
.arrow_box:after, .arrow_box:before {
    left: 100%;
    top: 50%;
    border: solid transparent;
    content: " ";
    height: 0;
    width: 0;
    position: absolute;
    pointer-events: none;
}

.arrow_box:after {
    border-color: rgba(136, 183, 213, 0);
    border-left-color: #88b7d5;
    border-width: 30px;
    margin-top: -30px;
}
.arrow_box:before {
    border-color: rgba(194, 225, 245, 0);
    border-left-color: #c2e1f5;
    border-width: 36px;
    margin-top: -36px;
}

Check it out on JSFiddle: http://jsfiddle.net/gpxfm6es/

The objective is to position this menu in the space between two elements on the left side. The challenge lies in placing the arrow pointing towards the gap between them.

In terms of JavaScript, the goal is to consistently position the menu between the hovered element and the one below it.

$( ".element" ).mouseover(function() {
  $( ".wrap" ).append( '<div class="arrow_box"></div>' ).css({top: , left: });
});

Answer №1

Visit this JSFiddle link for a live example

In this demonstration, I utilized the jquery .hover(mouseIn, mouseOut) function along with the .offset() method.

.hover():

$("elem").hover(function(){
// Code to execute when mouse enters
}, 
function() { 
// Code to execute when mouse exits
});

and .offset():

var position = $("elem").offset();
// The variable 'position' now contains an object that provides
// the CSS left and top of an element: position.left, position.top

Additionally, it is worth noting that .offset() and .position() largely serve the same purpose. While .position() offers the relative positioning of an element within its parent, .offset() gives you the absolute positioning within the document. This explains why in this instance, 'arrow_box' has an absolute position specified.

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

Showing JSON data in a list format

I am using Worklight and attempting to retrieve data from a database to populate a list. I have successfully retrieved the data using a SQLAdapter, but I am facing issues with loading it into a list on the user interface. Below is the data obtained from ...

Full height container with content that can be scrolled (Tailwind)

I am designing a webpage that requires two containers stacked on top of each other with some space in between. The bottom container should occupy the remaining screen space accurately, neither more nor less. Inside this lower container, there will be fixed ...

Enhancing validation in an established tutorial using Backbone

After following Thomas Davis' tutorial, I decided to enhance the age field by adding validation. However, my attempts to modify the Model as shown below have been unsuccessful: var User = Backbone.Model.extend({ validate: function(attr, error) { ...

Need help with renaming a column in the column selector window within jqGrid?

In my two-column header Phase1 & Phase 2 (Image 1), I want to display the column names (Image 2) in the column chooser window. Name Category Subcategory Category Subcategory I would like the display to be: Name Ph1 Category Ph1 Subcategory Ph2 ...

How to Send Data via Ajax in Laravel 5.6

I am facing an issue while trying to send data to a page using Ajax post without a form. It seems like the problem is related to CSRF, but I am unable to figure out how to resolve it. In the header of my page, I have the following: <meta name="csrf_to ...

Creating a server-side HTML template: A step-by-step guide

I need to create a consistent layout for all of my website pages using Bootstrap. While I already have a layout designed for a single page, I want to apply the same design across all pages. My initial thought was to utilize Jade for this task, but I want t ...

Is there a way to trigger a function in Javascript without manual intervention, similar to how

I've recently embarked on a journey to unravel the mysteries of jQuery. Consider this scenario where you want an action to be triggered when an anchor tag is clicked. Let's analyze the code snippet below: $('.selector').click The symb ...

How to prevent page scrolling in an Android web browser

Struggling to prevent my website from scrolling on different devices. While it's relatively easy to achieve on desktop browsers using overflow: hidden;, I'm facing issues with my Android tablet where the page continues to scroll no matter what I ...

Exploring the intricacies of Jquery in Tapestry weaving

I am using jquery/datatable and I want to implement highlighting for selected rows. I found sample jQuery code that accomplishes this: var oTable; var giRedraw = false; $(document).ready(function() { /* Add a click handler to the rows - this could be use ...

How can I retrieve the current background color and revert it back after a .hover event?

I am trying to use the .hover function to change the background color of a menu. Each menu item has a different background color, so I want it to return to its original color when the user hovers off. In other words, I want the script to read the current b ...

Displaying text values with a colored background is a feature of the TextInput component in React Native

Seeking assistance with creating a login screen using React Native, featuring semi-transparent text input. Encountering a peculiar issue where typed text appears highlighted, even though it is not. Please see the screenshot below: (Unable to upload to img ...

Discover the identity of an element through the value of another element

Looking for a way to retrieve the id value based on the input value in this HTML code. For instance, I need to identify the id value associated with the input value of number2 - which is unknown2. How can this be achieved using jQuery? <th id="unknow ...

Customizing individual characters within HTML text using the mouse's current location

Is there a way to manipulate the css properties of individual characters within a text string based on their proximity to the mouse cursor? If you're interested, check out this Codepen: https://codepen.io/NewbCake/pen/qYXvoo The concept involves tak ...

Exploring the implementation of toggling functionality for nested children within <li> elements using jQuery

Unable to get the toggle function working on child nodes. Can someone assist me with this issue? $(document).ready(function() { $('label.tree-toggler').click(function() { $(this).parent().children('ul.tree').toggle(300); }); ...

The accuracy of getBoundingClientRect in calculating the width of table cells (td)

Currently, I am tackling a feature that necessitates me to specify the CSS width in pixels for each td element of a table upon clicking a button. My approach involves using getBoundingClientRect to compute the td width and retrieving the value in pixels (e ...

elevate the div with a floating effect

My goal is to create a chat feature for users that will only be visible for a limited time period. I was thinking of using PHP and timestamp to achieve this functionality, but I also want to make the chat visually disappear by having the message div float ...

How to determine the exact placement following the dropping of a div into another div?

Here's a question that tells almost everything... Currently, I am utilizing jQuery's drag and drop features and encountering an issue. I have a div with the ID #one that can be dragged and dropped into another div with the ID #two. Everything i ...

handling null data in a jquery ajax callback

I've been working with knockout.js to bind data and making jQuery ajax calls to a restful service that returns JSON data. I've tried multiple approaches but can't seem to figure out the correct syntax for the call. Every time the callback fi ...

Tips for adjusting the value of a textbox up and down

I am facing an issue with my booking flight form that is supposed to take input from users regarding the number of travelers. I have three textboxes for Adult, Children, and Infants respectively, along with a main textbox to display the final result. Howev ...

The compatibility between Javascript and AJAX functions is currently not functioning as expected

I am attempting to send some data to the server using AJAX with the value obtained from a JavaScript variable. Here is the code snippet: <script type="text/javascript> var url; function applyPhoto(_src) { url = _src; var pho ...