Create a clear section within a div element

https://i.sstatic.net/TCp6E.jpg I'm looking to create a design similar to the image above using only 2 divs and an img. I was able to achieve it using 6 divs, but is there a way to make a transparent mapped area of the parent div with CSS or JavaScript?

Note: The middle square div should be draggable and resizable.

Answer №1

In this code snippet, I have utilized three separate div elements: one for the background image, another for a background overlay (to create a tint), and a cropped region.

The cropped region shares the same background-image as the background image div, but with the background position adjusted to negative values according to the dimensions of the crop.

const cropDiv = document.querySelector(".croppedRegion");

let setCropRegion = function(x1, y1, x2, y2) {
  cropDiv.style.backgroundPositionX = `${-x1}px`;
  cropDiv.style.backgroundPositionY = `${-y1}px`;
  cropDiv.style.width = `${x2-x1}px`;
  cropDiv.style.height = `${y2-y1}px`;
  cropDiv.style.left = `${x1}px`;
  cropDiv.style.top = `${y1}px`;
}

setCropRegion(10, 10, 240, 240);
* {
  margin:0px;
}

.backgroundImg {
  width:250px;
  height:250px;
  background-image: url('https://upload.wikimedia.org/wikipedia/en/e/ed/Nyan_cat_250px_frame.PNG');
  
  position:relative;
}

.backgroundOverlay {
  margin:0px;
  padding:0px;
  width:100%;
  height:100%;
  background-color:rgba(255,255,255,0.6);
}

.croppedRegion {
  width:110px;
  height:80px;
  background-image: url('https://upload.wikimedia.org/wikipedia/en/e/ed/Nyan_cat_250px_frame.PNG');
  
  background-size:250px 250px;
  background-position:-80px -90px;
  position:absolute;
  top:90px;
  left:80px;
}
<div class="backgroundImg">
  <div class="backgroundOverlay">
    <div class="croppedRegion"></div>
  </div>
</div>

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

Ways to postpone CSS and Script loading once JavaScript has been executed

My issue with my backbone.js app is that I have noticed some slow loading files (a .css and a .js) that are causing the page to block until they are fully loaded. After rendering the backbone view, I am looking for a way to delay the loading of these file ...

Attempting to simultaneously start my node.js server and React .js app, resulting in the error message 'ERR_MODULE_NOT_FOUND'

Currently facing issues while setting up a server for an ecommerce site being built in React. Attempting to run both the React app and the server simultaneously using npm run dev, but encountering various errors that persist even after attempted fixes foun ...

You cannot make a hook call within the body of a function component, unless you are using useNavigate and useEffect in conjunction with axios interceptors

Currently, I am delving into React and attempting to include a Bearer token for private API calls. My approach involves writing a private axios function to intercept requests and responses. Subsequently, I invoke it in my API.js file to fetch data from the ...

The onsubmit function encounters an issue with invocation when implemented with Node.js

When I submit the form, I want to perform some validations. However, it seems like the validation function is not working as expected. The alert part does not appear when triggered. Here is the code snippet: function validation(){ alert("somethi ...

What steps should be taken to refresh a page following an AJAX file upload?

I need some help creating a progress bar to track file uploads on my website. Here's what I have so far: HTML: <form action="upload/files.php" method="post" enctype="multipart/form-data" id="frmUpload"> <input type="file" name="upfile" ...

Submit an Ajax form to process data in PHP upon selecting a radio button

I am troubleshooting an issue with a form that is not submitting data to processor.php for processing and storing responses in a database. Ensuring that the form submission does not cause a page refresh is crucial, as there is an iframe below the form tha ...

What are some methods for equalizing the height of images in a flexbox layout when I am unsure of their individual dimensions?

I'm looking to create 2 images with different widths that are displayed at the same height side-by-side, preferably using a flex box. The method below works if I have the image dimensions (962x706 and 962x1275). Is there a way to achieve this withou ...

Retrieving the chosen option in Vue.js when the @change event occurs

I have a dropdown menu and I want to perform different actions depending on the selected option. I am using a separate vue.html and TypeScript file. Here is my code snippet: <select name="LeaveType" @change="onChange()" class="f ...

Challenges with the WebVR Boilerplate - Issues include the manager not displaying in VR mode, the camera being stuck looking straight down, and fullscreen mode only

I've been working on incorporating WebVR into a project that I created using Three.js, but I'm running into some issues with getting it to work properly. Several problems crop up when I try to view the scene: When VRMode is set to false, my ...

Replicate and customize identical object for creating instances in JavaScript

I am working with an object that has the following structure: var customObject = function() { this.property = "value"; }; customObject.prototype = new otherObject(); customObject.prototype.property2 = function() {}; This is just a snippet as the ac ...

Retrieving every item from an unnumbered inventory in PHP

I have a list of tag clouds: <ul #id = "tag-cloud-list"> <li class = "items">Music</li> <li class = "items">Lion</li> <li class = "items">Dwarf</li< </ul> This tag cloud list was generated using ...

How to send parameters to the jquery .css() method

I'm attempting to create hyperlinks that can dynamically set inline styles for different elements on a webpage. I have managed to save the element and attribute settings in hidden span elements and retrieve them to display in an alert dialog. However, ...

Is it possible to import multiple versions of a JavaScript file using HTML and JavaScript?

After extensive research, I am starting to think that using multiple versions of the same JavaScript library on a single page is not possible (without utilizing jquery Can I use multiple versions of jQuery on the same page?, which I am not). However, I wan ...

Working with a single quotation mark in JavaScript and HTML

This is a question that I have posed previously, and although it may seem straightforward, I have yet to receive a response. I am looking for a way to include a single quote (') in a string. While I am aware that using double quotes can circumvent t ...

Safari not fully supporting CSS translate functionality

When I click a button on my app, an element slides into view and then slides out when I click the button again. This is achieved using a combination of CSS and JS. It works perfectly in Chrome and Firefox, but only partially in Safari. In Safari, the elem ...

The click event will not be triggered if the element is removed by my blur event

My dropdown list is dynamic, with clickable items that trigger actions when clicked. Upon focus, the list displays suggested items When blurred, the list clears its contents The issue arises when blur/focusout events are triggered, causing my element to ...

Load full html content, including doctype, into a jQuery container | Block scripts from running in iFrames

I am attempting to fetch an entire HTML file using AJAX and then manipulate the DOM with jQuery. This means that the retrieved HTML document includes a doctype and other top-level elements. The process of retrieving the HTML is straightforward: $.get(&ap ...

javascript has a funny way of saying "this is not equal to this" (well,

Sample 1 var Animal = function () { var animal = this; this.makeSound = function() { alert(animal.sound); } } var dog = new Animal(); dog.sound = 'woof'; dog.makeSound(); Sample 2 var Animal = function () { this.makeSound = ...

Wrap each object in a container and then insert its key and values into that container using jQuery

How can I wrap each item and then insert the object's indexes and values into each created wrapper? I've attempted to accomplish this using the following code: $.ajax({ url: "some url", type: "GET", success: function(data) { var data ...

How can we identify if a React component is stateless/functional?

Two types of components exist in my React project: functional/stateless and those inherited from React.Component: const Component1 = () => (<span>Hello</span>) class Component2 extends React.Component { render() { return (<span> ...