Setting a customized background color for a designated section of a component

I am trying to create a hover effect at the position of a cursor, similar to what is shown in this example:

For reference, I have also created a code snippet: https://jsfiddle.net/onnmwyhd/

Below is the code I am using:

HTML

<div id="container"></div>

CSS

#container{
background-color: #6fc39a;
height:200px;
}

jQuery

$("#container").mousemove(function(e){
var x = e.pageX - this.offsetLeft;
var y = e.pageY - this.offsetTop;
$(this).html("X: " + x + " Y: " + y); 
});

The desired color for the cursor position is:

background-image: -webkit-linear-gradient(-35deg, #35a28e, #a8e4a5);

Answer №1

If you're curious about how canvas is used on the reference website, take a look at this fiddle for an accurate representation:

JSFiddle

HTML

<div id="container" class="stalker">
    <canvas id="canvas" width="1600" height="433"></canvas>
</div>

CSS

.stalker {
    background-color: #6fc39a;
    height:200px;
    border-top-color: rgba(168, 228, 165, 0.7);
    border-bottom-color: rgba(53, 162, 142, 0.3);
}

JavaScript

var stalker = $('.stalker');

var canvas = $('#canvas')[0];

var ctx = canvas.getContext('2d'), gradient, initialized = false;

$("#container").mousemove(function(e){
    setTimeout(function(){
        initialized = true;
        canvas.width  = stalker.width();
        canvas.height = stalker.height();
        gradient = ctx.createRadialGradient(e.pageX, e.pageY, 0, e.pageX, e.pageY, canvas.width);
        gradient.addColorStop(0, stalker.css('border-top-color'));
        gradient.addColorStop(1, stalker.css('border-bottom-color'));
        ctx.fillStyle = gradient;
        ctx.fillRect(0, 0, canvas.width, canvas.height);

    }, initialized ? 200 : 0);
});

Answer №2

Consider incorporating a span element within #container to store cursor values without altering the existing html content of the element. Additionally, include a div element inside #container with its css properties such as position set to absolute, left positioned at x, and top positioned at y for tracking the cursor with the div.

$(function() {
  $("#container").mousemove(function(e) {
    var x = e.pageX - this.offsetLeft;
    var y = e.pageY - this.offsetTop;
    $("div", this).css({
      left: x - (75 / 2),
      top: y - (75 / 2)
    })
    $("span", this).html("X: " + x + " Y: " + y);
  }).mousemove();
})
#container {
  background-image: -webkit-linear-gradient(-35deg, #35a28e, #a8e4a5);
  background-image: linear-gradient(125deg, #35a28e, #a8e4a5);
  background-color: #6fc39a;
  height: 200px;
}
#container div {
  background-image: -webkit-linear-gradient(-35deg, #35a28e, #a8e4a5);
  width: 75px;
  height: 75px;
  position: absolute;
  border-radius: 100px;
  opacity: 0.5
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>

<div id="container">
  <span></span>
  <div></div>

</div>

Explore the code on JSFiddle https://jsfiddle.net/onnmwyhd/2/

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

Tips for customizing the appearance of dayMouseover in Fullcalendar

While working on my Angular application, I encountered an issue with the Fullcalendar plugin. Specifically, I want to dynamically change the cell color when hovering over a time slot in AgendaWeek view (e.g. 7.30am-8.00am). I am striving for something like ...

Choosing a sibling element before another using CSS

Is there a way to make both of my icons display some text when hovered over? I managed to make it work, but the leftmost icon doesn't trigger the span element when hovered. The text needs to appear on the left side of the Yelp icon for design reasons. ...

Discover a method to retrieve all recommended strings based on a search query using JavaScript

I have two strings: one containing all the values of countries and the second string that I entered when searching for a country. Now, I am looking to retrieve all results that contain "In", such as India and Indonesia. For example, if I search for "IN" ...

The iPhone encountering issues with Safari's interpretation of @media queries

When it comes to my website on a desktop screen, the body text should not be at the top of the page. However, on an iPhone, it must be displayed at the top. I've created a class with empty paragraphs to control the display on and off, but this class s ...

Having issues with UTF8 encoding when utilizing AJAX to send the complete HTML source in JavaScript

I'm encountering difficulties when trying to send a complete page source using AJAX. Despite attempting to escape the content with escape(), encodeURI(), and encodeURIComponent(), I cannot successfully transmit utf8 characters. Below is my code snipp ...

How can one safeguard their JavaScript code from potential threats and unauthorized access?

How can I ensure that a web app is not accessible or operated from the local file system? The app has a custom front-end workspace created with JS and various libraries, which should only be usable when the user is logged in to the domain with an active i ...

Tips for saving data obtained from an ajax call

My jquery ajax function has a callback that creates an array from retrieved json data. Here's an example: success: function (response) { callback(response); }, The callback function, in this case createQuestionsArray(), populates ...

What steps are involved in bundling a Node project into a single file?

Recently, I've been using npm to create projects. When it comes to AMD, I find the native node require to be very convenient and effective for my needs. Currently, I rely on grunt-watchify to run my projects by specifying an entry file and an output f ...

Error in JSON access permissions while using AJAX call to Webmethod in ASP.NET webforms with jquery Datatables

My current project involves implementing server-side processing for jQuery Datatables.NET in an ASP.NET webforms test application. Here is the code I am using: $(document).ready(start); function start(){ $('#PersonsTable').DataTable({ ...

Is there a way to transform a string property into a custom type in a TypeScript array?

I am faced with a situation where I have an interface that is extending a MongoDB document, along with some sample data that is also extending that interface. Below is an outline of the interface: export default interface IModel extends Document { _id: Obj ...

Is it possible to separate a portion of HTML into a template and reuse it in multiple locations within a webpage?

In my HTML, I have an issue with duplicate code. In Java, I typically use IntelliJ to mark the code and select "extract method" => "replace 2 occurrences". I am looking for a similar solution in HTML, but the current method seems messy: <ng-template ...

"document.createElement encounters an issue when used for creating a new window

I am currently working with two different HTML files: FirstWindow and SecondWindow. The FirstWindow is linked to FirstWindowJS.js, while the SecondWindow is linked to SecondWindowJS.js. The issue arises when I try to open SecondWindow.html using FirstWind ...

Overridden CSS rules

Having a slight CSS dilemma. Within my webpage's html, I have the following structure: <div class='box-div'> <div>Entry 1</div> <div class='hide'>Entry 2</div> </div> Here is my associated ...

Create a bootstrap table with td elements that have no wrap for their content

I am currently developing an application using springboot, thymeleaf, and bootstrap. Here is the snippet of the html code I'm working with: <!doctype html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <h ...

Exploring the functionality of ISO 8601 periods with JavaScript

Has anyone successfully compared ISO 8601 periods in JavaScript to determine which period has a longer duration? For example, is P6M (6 months) longer than PT10M (10 minutes)? I haven't been able to find any built-in solutions for this. Any suggestio ...

Django - tallying timer in action

Looking to add a timer that begins when accessing this template: {% block content %} <h3>C-Test</h3> <p>In the text below, some word endings are missing. The gap is about half the length of the word, so you need to fill in the rest ...

Can Vue.js be configured to reload specific components only?

Can a specific component be reloaded in a Vue component that contains multiple components? For example, if there is a component structured like this: Main component <template> <button>Button<button> <component1></component> ...

"Utilizing JSON data to implement custom time formatting on the y-axis with AmCharts

Looking to convert minutes to hh:mm:ss format in my JavaScript code var allDataTime = [{ date: new Date(2012, 0, 1), "col": "LONG CALL WAITING", "duration1": '720', "duration2": '57', "duration3": ...

Tips for configuring options within a jQuery widget factory using data retrieved from server-side variables stored in a JSON file

I am looking to configure the settings for a widget using JSON file variable values. How can I achieve this and how do I transfer the JSON file to the client side? The following code snippet is taken from the jQueryUI Widget Factory <script> $(f ...

I am experiencing an issue with my Jquery file not being able to locate the Controller

I'm having trouble passing data from the view to my controller. I've used a similar syntax in CodeIgniter and it worked perfectly. Any suggestions? function Navigation() { $(".getDataId").on("click", function(){ var dataId = $(this) ...