Conceal/Inactivate element when scrolling, and once scrolling comes to a halt, reveal it once more

I have been working on creating a div tag to overlay an embed tag and added some javascript to prevent users from right-clicking to download the pdf inside the embed tag (even though it may seem unnecessary, it was requested by my customer). Here is the main issue: I want users to still be able to scroll through the pdf. I set up the scrolling functionality so that every time a user scrolls, the div tag mentioned above disappears, allowing users to scroll through the pdf. However, when they stop scrolling, the div tag reappears to prevent users from right-clicking on the pdf. This is what I have implemented so far:

var wheeling;
window.addEventListener('wheel', function (e) {
        if (!wheeling) {
            console.log('start wheeling!');
            document.querySelector("#lesson_pdf").style.display = "none";
        }

        clearTimeout(wheeling);
        wheeling = setTimeout(function() {
            console.log('stop wheeling!');
            document.querySelector("#lesson_pdf").style.display = "block";
            wheeling = undefined;
        }, 300);
    });

Here is my HTML structure:

<div>
    <div style="position: relative;">
        //the embed tag I want to cover
        <embed src=" {{ getImageFile($pdf_src) }}" class="tw-w-full tw-h-[500px] pdf-reader-frame" style="position: absolute;">
    </div>
    //The div tag mentioned earlier
    <div style="position: absolute;
                height: 100%;
                width: 100%;
                display: block;
                z-index: 1;" id="lesson_pdf">
    </div>
</div>

The problem is, the current setup is not functioning as intended. Sometimes I have to scroll multiple times before being able to scroll through the pdf file. Can anyone assist me in resolving this issue?

Answer №1

Instead of creating a div overlay to prevent users from right-clicking on a PDF, why not just disable the context menu? This can be achieved using the contextmenu event, which is more efficient than toggling the visibility of an overlay (unless specifically requested by the client).

const img = document.querySelector("img")

img.addEventListener("contextmenu", (e) => {
  e.preventDefault();
  return false
})
<img src="http://via.placeholder.com/640x360" />

Answer №2

If you're looking for some helpful code, look no further! Just be sure to include the necessary CSS.

var preventElement = document.getElementById('embed-document');
preventElement.addEventListener("contextmenu", (e) => {
e.preventDefault();
});
embed {
            background:#cccccc;
            pointer-events: none;
        }
<div>
    <div id="embed-document">
      <embed src="https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf" class="tw-w-full tw-h-[500px] pdf-reader-frame">
    </div>
</div>

Answer №3

If you only have a small number of PDFs to display, this method could work well.

Scroll PDF embedded in HTML

One option is to place the iframe within a div with overflow: auto, setting the height of the iframe to be very large so the PDF displays at full size. Keep the surrounding div shorter than the iframe and scroll it when needed.

The drawback is that you'll need to manually adjust the height of the embed element to match the full height of the PDF with all pages visible.

Running the code snippet on Stackoverflow using Chrome may not work due to some sandbox plugin errors. However, it worked for me outside of a sandbox (using VS Code and live server) and also on Firefox developer version. I tested it with an embed tag and it functioned properly.

.wrapper {
  position: relative;
  overflow: auto;
  
  /* Set your desired height for the PDF viewer */
  height: 500px;
}

.embed-cover {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: 1000;
  
  /* Should match embed height */
  height: 1000px;

  /* Just for visual demonstration, remove in practice */
  background: red;
  opacity: 0.25;
}

embed {
  width: 100%;
  
  /* Should match embed-cover height */
  height: 1000px;
}
<div class="wrapper" onContextMenu="return false;" >
  <div id="embed-cover" class="embed-cover">asd</div>
  <embed src="https://www.orimi.com/pdf-test.pdf" />
</div>

Answer №4

Not looking to dampen anyone's spirits with my response, but it's important to consider that while some solutions may help protect those who are vulnerable, like the disabled, they may not be fully accessible for everyone (not DDA/508 compatible). It's crucial to remember that determined individuals will find ways to access content regardless of barriers in place, so blocking access altogether is ineffective and can even be seen as counterproductive.

The key aspect of PDF handling lies in its ability to be openly used as a public domain standard, outlined in the fine print when using these types of files.

An interactive PDF processor engages with users during file processing, allowing for both reading and writing capabilities within the PDF format.

In essence, full accessibility should be a priority for any compliant PDF editing or reading software.

https://i.stack.imgur.com/EErXP.png

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

Utilize the double parsing of JSON information (or opt for an alternative technique for dividing the data

Looking for the most effective approach to breaking down a large data object retrieved from AJAX. When sending just one part (like paths), I typically use JSON.parse(data). However, my goal is to split the object into individual blocks first and then parse ...

Exploring the process of setting up Jasmine tests for both services and controllers in Angular.js

I have been facing challenges in creating accurate tests for my Angular services and controllers, even though they are functioning correctly. I have searched extensively for a solution to this problem. Below is an example of my controller: angular.module ...

Ensuring text is perfectly centered within a label and creating a label that is entirely clickable

Struggling to style a file input button and encountering some issues with labels. Here is the button in question. Firstly, how can I center the text in the label? Secondly, how can I make the entire button clickable instead of just the upper half? Also, ...

Strategies for avoiding a hover component from causing distortion to its parent component in React

I am encountering an issue with a hover component that is causing distortion in its parent component when displayed. Essentially, I need to prevent the hover component from affecting the layout of its container. Below is the code snippet: Styling for Lang ...

Page title missing from browser tab

I don't come from a design background, so the issue I'm experiencing is quite peculiar. Below is a snippet of code from MVC4 - <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-C ...

How to efficiently eliminate duplicates from an array list using React framework

Keeping the array name constant while duplicating and repeating this process only clutters the list. Appreciate your help. setListItems(contents.data); console.log(contents.data); ...

When a link is right-clicked, the window.opener property becomes null

Currently, I am utilizing the window.opener property in JavaScript to update the parent window from a child window. The parent window simply consists of a table with data and a link to open the child window. When the child window is launched, a JavaScript ...

Only displaying sub items upon clicking the parent item in VueJS

I'm in the process of designing a navigation sidebar with main items and corresponding sub-items. I want the sub-item to be visible only when its parent item is clicked, and when a sub-item is clicked, I aim for it to stand out with a different color. ...

I would like to exclude the item within my ng-repeat loop using ng-if

I'm attempting to utilize ng-if within ng-repeat in order to create Accordions. Depending on the condition value, I want certain items to be skipped in the ng-repeat. For example, if item.condition is true, then only display the accordion. The code b ...

Changing the State of a CheckBox with ReactJS and Material UI

Utilizing Material UI's Checkbox, I am creating a form to input data and update or add values into a state object. This form serves the purpose of both editing an existing holiday or adding a new one. I'm currently facing an issue where the stat ...

I'm stumped trying to understand why I keep getting this syntax error. Any thoughts on how to fix it?

Our team is currently working on creating a dynamic SELECT box with autocomplete functionality, inspired by the Standard Select found at this link: We encountered an issue where the SELECT box was not populating as expected. After further investigation, ...

tag-swapping function

I have a specific HTML code paragraph that I am working with: <p id=tag1> html statements before click </p> My goal is to create a function that will split the paragraph into two separate paragraphs like so : <p id=tag1> html statement ...

Error message: Attempting to access the 'path' property of an undefined variable results in TypeError while utilizing cloudinary services

I am currently attempting to upload a file from React to an Express server, but I keep encountering this error TypeError: Cannot read property 'path' of undefined The method I am using involves React for transferring/uploading the file to the ...

Error: Attempting to access the 'body' property of an undefined object following a POST request in a MEAN application

Currently, I am attempting to send POST data from AngularJS to Express. My approach involves using curl to transmit the POST content. After sending the data and receiving a 200 response, I encounter an issue when trying to access the data through body-par ...

Having Difficulty Formatting HTML Input in Next.js

I'm encountering difficulties when trying to style HTML form elements in Next.js. I've been unsuccessful in applying CSS using a simple class name from the imported stylesheet, but have managed to achieve some success with inline styling by using ...

Navigating through two nested arrays in JavaScript to access an object

Having difficulty extracting the nested car values using JavaScript (lodash). Take a look at the JSON data below: { "cars":[ { "nestedCars":[ { "car":"Truck", "color" ...

Utilizing 'Ng-If' causes a glitch in the program during the execution of a $( '#x' ).change event or when adding a new item with AngularFire $add

After implementing ng-if in my code, I observed two specific instances where it caused unexpected behavior. The first instance involves using ng-if="isOwnProfile" for an image-upload toolbar. However, the use of ng-if resulted in the event listener ceasin ...

The Google Language Translator disregards the formatting

Currently, I am in the midst of developing a website with Wordpress and have successfully integrated Google Language Translator. Everything seems to be working well, except for one issue - when I translate the content, it alters the font size. Prior to tra ...

When the button is clicked, the image vanishes into thin

One common issue is the image disappearing when users click on the Rotate Anti-clockwise or Rotate Clockwise buttons. This can be a frustrating problem to tackle! Check out this link for more information. If you run into this issue, here are some tips: ...

How can I integrate a timer into an Angular carousel feature?

I have put together a carousel based on a tutorial I found on a website. Check out the HTML code for my carousel below: <div class="row carousel" (mouseover)="mouseCheck()"> <!-- For the prev control button ...