Is there a way to have a div grow in size as I move the viewport?

My goal is to create a smooth panning effect within a viewport using a child <div>. However, I want the child <div> to always fill the viewport completely, without any edges visible. Essentially, I am looking for a way to make the panning experience "endless" by continuously adding onto the child <div> when the user approaches the edge of the viewport.

UPDATE: Just to clarify, my intention is for the child <div> to remain stationary while allowing users to pan around freely. The idea is to create an illusion of an infinite plane that users can explore without any fixed elements in play.

Answer №1

After some trial and error, I managed to resolve this issue by introducing a separate element that mirrors the grid background, while also ensuring that the background moves along with the child <div>. Take a look at the snippet below:

var isDragging = false;
var lastMouseX;
var lastMouseY;

$("#viewport").mousedown(function(ev) {
  if (ev.which == 1) {
    isDragging = true;
  }
});

$("html").mouseup(function(ev) {
  if (ev.which == 1) {
    isDragging = false;
  }
});

$("#viewport").mousemove(function(e) {
  var deltaX = lastMouseX - e.clientX;
  var deltaY = lastMouseY - e.clientY;

  lastMouseX = e.clientX;
  lastMouseY = e.clientY;

  if (isDragging) {
    $("#view").css({
      left: "-=" + deltaX,
      top: "-=" + deltaY
    });
    $("#grid").css({
      'background-position-x': "-=" + deltaX,
      'background-position-y': "-=" + deltaY,
    });
  };
});
body {
  background-color: grey;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

#viewport {
  display: block;
  background-color: transparent;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

#view {
  position: relative;
  width: 100%;
  height: 100%;
}

#grid {
  position: fixed;
  width: 100%;
  height: 100%;
  background-size: 24px 24px;
  background-image: linear-gradient(to right, white 1px, transparent 1px), linear-gradient(to bottom, white 1px, transparent 1px);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="viewport">
  <div id="grid"></div>
  <div id="view"></div>
</div>

Answer №2

Experiment with adjusting the dimensions and location of the container

if (isDragging) {
  $("#container").css({
    left: "-=" + deltaX,
    top: "-=" + deltaY,
    width: "+=" + deltaX,
    height: "+=" + deltaY
});

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

Struggling to resolve issues with out-of-Viewport elements in my code while using Python 3.7 and Selenium

My current challenge involves troubleshooting my code to resolve the issue preventing me from utilizing the "actions.move_to_element" method to navigate to an offscreen element and click on it. The specific line of code I am focusing on is: Off_Screen_Ele ...

The functionality of the jQuery script is not operating optimally, causing the expected alert to not be displayed

I implemented this jQuery script: $('input[name="add-post"]').on('click', function(form) { form.preventDefault(); for ( instance in CKEDITOR.instances ) CKEDITOR.instances[instance].updateElement(); $.ajax({ typ ...

How can I customize the border color in Bootstrap with a specific hex code?

Being new to the world of web design, I have a burning question that may have an easy solution that has evaded my notice. In my current project, I am attempting to create a div with a colored border using bootstrap. Specifically, I want to use a specific h ...

Reload the precise URL using JavaScript or jQuery

I need a solution to refresh the current URL after an ajax success. I attempted the following methods: location.reload() history.go(0) location.href = location.href location.href = location.pathname location.replace(location.pathname) However, I encounter ...

Results from various calculations for time retrogression will remain constant once they are shown

I have a challenge where I need to display different types of dates based on the current day and time. The catch is that once the output is generated, it should remain static even if the actual time changes. For instance, in box 1: Let's say it&apos ...

Removing OTP user input upon pressing the Backspace key can be achieved through the following steps

I've developed an OTP component but I am encountering two specific issues that are posing a challenge. The first problem arises when I hit the Backspace key - I want the input value to be deleted, followed by automatically moving to the previous inpu ...

Enhancing your react-slick carousel with a captivating full-screen background

I need assistance with implementing a full screen slider using react-slick. I am facing difficulty in adding an image as the background. My code snippet looks like this: import image1 from "../assets/bg1.png" import image2 from "../ ...

Incorporating components into Vue while utilizing Flask poses a challenge

Currently, I am working on a project that involves Flask and Vue. I am attempting to add a carousel to my website, but I am running into difficulties with creating and importing components for Vue when following tutorials. Here is my current file structure ...

Secure Routes with Moralis for React Router v6 - A Powerful Combination

I'm having trouble figuring out why this piece of code isn't functioning as expected. The variable "isAuthenticated" comes from the Moralis API and is a boolean value. Ideally, if it's true, it should display the "Outlet", and if it's f ...

Utilizing Anglar 16's MatTable trackBy feature on FormGroup for identifying unaltered fields

In my application, I am working with a MatTable that has a datasource consisting of AbstractControls (FormGroups) to create an editable table. At the end of each row, there are action buttons for saving or deleting the elements. My goal is to implement tr ...

How to implement scroll spy in Bootstrap 4 to highlight parent li elements?

I have a bootstrap4 menu set up like this: <ul class="navbar-nav ml-auto"> <li class="nav-item"><a class="nav-link" href="#introduction">INTRODUCTION <span class="sr-only">(current)</span></a></li> </ul> Th ...

Table data is being displayed from form data in another file using only JavaScript

I am trying to figure out how to use local storage to store form data in one HTML file and display it in a table on another HTML file. The form data should be stored in the columns of the table but on a separate page. Can anyone provide assistance with thi ...

My collection consists of objects arranged in this manner

let attributeSet = [{ "id": 1, "value": 11 }, { "id" : 1, "value": 12 }, { "id" : 1, "value" : 13 }, { "id": "2", "value& ...

The sidebar appears to be hovering above the footer section

I am currently working on creating a sidebar that sometimes ends up being longer than the main content section. You can check out my demo here: http://jsfiddle.net/y9hp4evy/1/ and another case here: http://jsfiddle.net/y9hp4evy/2/ (If I add a height to th ...

The Canvas feature does not support saving server side PHP code

I am facing an issue with saving a canvas to a directory and storing the URL in a database. Separately, saving the file or storing the URL in the database works fine. But when I combine both actions using AJAX to specify a PHP file, the session variable i ...

Identifying Seating Arrangements at the Poker Table - Sit-and-Go Events

Note: The JavaScript code has been implemented based on the answer provided by ajrwhite. Hopefully, it proves helpful to someone. Link: http://codepen.io/eMineiro/pen/EKrNBe Be sure to open the CodePen console to see the examples in action. When playing ...

Setting up meta tags in React for optimal search engine optimization

In the process of developing a React app with react router, I encountered the need for SEO optimization and adding meta tags to each page. Despite trying out react helmet, it seems that the Yandex micromarkup validator (located at https://webmaster.yandex. ...

Discovering the absent objects within an array of objects through comparison

I may need some guidance on this, so any suggestions are appreciated. Currently, I have developed a web scraper using Node.js that extracts a list of job postings from our organization's website. These jobs are stored as an array of objects, which is ...

Tips for modifying a data entry in LocalStorage within React functional components?

I am a beginner in React JS, and I am attempting to modify some values from LocalStorage. Despite seeing the updates in the console, the changes are not reflected on the front end. I have gone through similar threads, but none of them seem to provide the r ...

Radiant UI: Radio Button featuring a stylish svg circle

Hey everyone, I’ve been attempting to customize the Material-UI radio button using CSS modules but haven’t had much luck. It seems like the easiest solution would be to use the makeStyles function from Material-UI, however, I am required to work with C ...