Floating Crosshair within Div - Limited to Container

I'm attempting to implement a crosshair-style cursor effect on a div that houses a D3 Datamap. I've managed to achieve this using jQuery, but the crosshairs appear to extend beyond the boundaries of their parent div on the bottom and right sides, while staying within those boundaries on the top and left.

To illustrate, I have put together this fiddle.

Despite my efforts to adjust the position property of the crosshair div, I haven't been able to resolve this issue.

Interestingly, when I set the position to absolute on my website, it does confine the crosshairs correctly within the container, but there is an offset in the center point from the actual cursor coordinates (e.pageX, e.pageY). Unfortunately, these results cannot be reproduced in the fiddle since changing between

fixed, static, relative, absolute
doesn't seem to make a difference.

One workaround I discovered was to assign fixed values for the width and height of both the container and the crosshairs. However, this poses an issue as I require the container to be responsive.

Answer №1

Initially, the vertical and horizontal lines were overlapping the box on the top and left side, but they were not visible due to the body-viewport.

After conducting some research, I discovered that the optimal solution would involve placing the crosshair-lines inside the map-container with absolute positioning relative to the map-container. This approach allows for better control over the position and behavior of the crosshair-lines, while also ensuring flexibility for the map-container!

To facilitate testing, I included the default cursor. - Check out the code snippet at: https://jsfiddle.net/9r4rtcz9/6/

//Map Hover Crosshairs
$(function() {
  var cH = $('#crosshair-h'),
    cV = $('#crosshair-v');
  $('.map_wrapper').on('mouseover', function() {
    cH.css('visibility', 'visible');
    cV.css('visibility', 'visible');

    $('.map_wrapper').bind('mousemove', function(e) {
      cH.css('top', e.pageY);
      cV.css('left', e.pageX);
    });
  });

  $('.map_wrapper').on('mouseout', function() {
    cH.css('visibility', 'hidden');
    cV.css('visibility', 'hidden');
    $('.map_wrapper').unbind("mousemove");
  });
});
.map_wrapper {
  position: relative;
  height: 100px;
  width: 300px;
  border: 1px solid black;
  overflow: hidden;
  cursor: default;
}

.hair {
  float: left;
  position: absolute;
  background-color: rgba(100, 100, 100, 0.5);
  z-index: 10;
  pointer-events: none;
}

#crosshair-h {
  width: 100%;
  height: 2px;
  margin-top: -8px;
  visibility: visible;
}

#crosshair-v {
  height: 100%;
  width: 2px;
  margin-left: -8px;
  visibility: visible;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="map_wrapper">
  <div class="hair" id="crosshair-h"></div>
  <div class="hair" id="crosshair-v"></div>
</div>

<p class="datamaps">D3 Data Maps Here</p>

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

Personalized Tumblr-style button

Hey there, I've been working on creating a custom Tumblr-like button and tried adding an iframe above it to make it clickable. However, something seems to be off as it's not functioning properly. I'm still trying to wrap my head around how i ...

unable to enforce max-height using bootstrap

Hello everyone, I have a website built using Bootstrap and I've created a simple header file that includes styling for the navigation bar, fonts, and layout. However, I'm facing an issue with making a video span 100% of the browser width with a m ...

Increasing the size of the center image by using CSS elements placed along the circumference

Is there a way to enlarge the center image by 1.25 times compared to the outer images? You can view the code on this codepen. For a simplified explanation of the code, check out this link. I have attempted to adjust the circle size but it ends up affectin ...

Is it possible to create a HTML 5 tile with a contemporary Metro-style UI?

I am curious to know if it is possible to replicate the tile system found in the modern UI of Windows 8 on an HTML page. I have already created a demo based on a website. You can view it here. Below is the code snippet: <div class="tile pos1-1 w1h1 te ...

The functionality of jQuery is not functioning properly on dynamically created identical divs

I am currently working on creating a comment reply section. I have managed to load the same div for reply that I use for commenting by utilizing $('#1').append($('.enterComment').html());. In this case, 1 represents the id of the div th ...

"The Vuetify calendar widget is cutting off some dates at the end of the month

Currently, I am using the vuetify date picker in my project. The API that I am interfacing with will showcase data within a specific date range. I have utilized the date picker component along with the range prop to achieve this functionality successfully. ...

Background of jQuery-UI Slider

Can the background color of a jQuery-UI Slider widget be set using JavaScript? This is the default setting: What I am trying to accomplish is the following: The green range should be determined based on historical data. I want to visually show the user ...

What is preventing me from getting this straightforward CSS transition to function properly?

I'm really struggling with CSS transitions and trying to figure out why my transition for the div class=text is not working as expected. const menuToggle = document.querySelector('.toggle') const showCase = document.querySelector('.s ...

Ion-row appears out of frame

This is a simple layout: <ion-content> <ion-grid *ngIf="loaded"> <ion-row> <ion-col>Stuff</ion-col> <ion-col>Stuff</ion-col> <ion-col>Stuff</ion-col> </ion-row> < ...

I'm having trouble applying CSS stylings to my components in my React project. What could be the issue causing this?

As a React beginner, I am facing issues with getting my CSS to work properly in my project. I know that CSS is not used directly in React, but rather interpreted as JavaScript using webpack and babel. What have I tried? I attempted to make changes to my w ...

Assign the class name of the clicked div to another specific div

I am working on a function component where I want to append a class name to a specific div when a user clicks on one of 4 different divs. The divs are named note_b, note_g, note_p, and note_y. Below is the code snippet that I have so far: import React fro ...

Place a JavaScript code block within a designated div container

I'm currently working on revamping the appearance of the header area for this website. My goal is to replace the background image with a rectangular JavaScript ad positioned to the right of the logo. However, I've encountered difficulty due to th ...

Complete Navigation Bar Expands Alongside Drop-down Menu

For my navbar, I am attempting to set a custom breakpoint at 1080px where it switches to a hamburger menu. However, when I click on a dropdown option, the entire navbar expands instead of just the dropdown area. @media (min-width: 1080px){ .navbar-expand- ...

Manipulate elements by adding and removing classes sequentially based on an array

Previously, some had difficulty understanding my question. I have an array of variables representing the ids of 5 divs. My goal is to change the color of each div sequentially for a brief moment before reverting back, mimicking the behavior of traffic ligh ...

Assign position values to an element inside a DIV container using JavaScript, Cascading Style Sheets, and

I am currently creating some visual representations of journal issues. I am looking to showcase blocks of text using basic DIVs or other elements inside another DIV, mirroring the organization of the issue page. In order to achieve this, I need to positi ...

How can I ensure my md-sidenav takes up the entire height when using ui-router?

Currently, I am utilizing the most recent version of Angular Material (1.0.0) library and I am attempting to ensure that my <md-sidebar> panel occupies the entire height of the available space on the webpage. While it functions correctly on a regul ...

Is there a way to adjust the text color based on whether a value is negative?

I am currently developing a web application that performs addition operations on integers. Within this application, I have implemented two functions named num1() and num2() to retrieve the integers provided by the user for calculation. My objective is to m ...

Utilizing a ListView Below a LinearLayout: Step-by-Step Guide

In my Android project, I am working on creating the MyProfile activity. This activity will display a user's profile picture, name, and other bio data on the first screen. Additionally, I would like to make the layout swipeable so that when the screen ...

What is the best way to clear and fill a div without causing it to resize?

I am faced with a challenge involving four thumbnail divs labeled .jobs within a #job-wrap container. When a .job is clicked, I want the #job-wrap to fade out, clear its contents, load information from the selected .job, and then fade back in. However, whe ...

What could be causing the width of my aside not to display as I intended?

I've successfully used Flex multiple times in the past, but for some reason, I'm encountering an issue now. I isolated the problematic code in a separate HTML file, yet the problem persists. In my layout, I have a container div with an aside and ...