Firefoxy can't seem to get through the navigation keys within an element that has a hidden

Check out this fiddle I created: http://jsfiddle.net/c11khcak/13/. My goal is to hide the scrollbar of an element while still allowing it to be scrollable using the mouse wheel and up/down navigation keys. I've been able to achieve this using some basic CSS:

.parent{
    position: relative;
    width: 300px;
    height: 150px;
    border: 1px solid black;
    overflow: hidden;
}

.child{
   height: 150px;
   width: 318px;
   overflow-y: scroll;
}

img {
    width: 318px; 
}

Everything works perfectly in Webkit browsers, but in Firefox, although scrolling via mouse wheel works, pressing the up/down keys does not work. Does anyone know how to fix this issue in Firefox?

Answer №1

The issue is with the focus flag (Reference Here) of the div.

To ensure that the div receives focus when clicked, you need to assign it a tabindex. The choice between using a negative, zero, or positive tabindex depends on your specific requirements.

If the value is a negative integer, the user agent will set the element's tabindex focus flag but prevent sequential focus navigation to it.

If the value is zero, the user agent sets the element's tabindex focus flag, allows for sequential focus navigation, and follows platform conventions for determining relative order.

If the value is greater than zero, the user agent sets the element's tabindex focus flag, enables sequential focus navigation, and includes the element in the sequential focus navigation order.

To make your example work, simply add:

<div class="child" tabindex="0">
   ...
</div>

Demo Fiddle: http://jsfiddle.net/abhitalks/y1boh9v7/3/

This solution should function correctly across all browsers (tested on IE-11, GC-39, and FF-34).


Below is an excerpt showcasing a method to hide scrollbars. You can hide scrollbars completely by increasing padding, although this example keeps them visible for simplicity. If you choose to hide scrollbars, provide an alternative scrolling mechanism like mouse-based drag-to-pan functionality using JavaScript.

In this snippet, the image pans when clicking the scrollbars, clicking the image and navigating with arrow keys, or touch-sliding.

Snippet:

div.scrollParent {
    height: 240px; width: 240px;
    border: 1px solid #eee;
    overflow: hidden;
}
div.scrollChild {
    height: 240px; width: 240px;
    padding: 0px 12px 12px 0px;
    overflow: auto; 
}
img { display: block; }
<div class="scrollParent">
    <div class="scrollChild" tabindex="0">
        <img src='http://lorempixel.com/320/320' />
    </div>
</div>

.

Answer №2

It seems that the issue arises because Firefox does not automatically focus on the .child element when clicked. A workaround for this is to use JavaScript:

$(function() {
    $('.child').on('click', function(e) {
        $('.child').focus();
    });
});

By clicking inside the child element, it will gain focus and allow you to navigate with the arrow keys.

Check out the demo here: http://jsfiddle.net/c11khcak/14/ (tested in Firefox 36.0a2)

Answer №3

To achieve this functionality, follow the steps below:

HTML (to enable key operations when div is clicked/focused)

<div class="child" tabindex='1'>

CSS

.parent{
  position: relative;
  width: 300px;
  height: 150px;
  border: 1px solid black;
  overflow: hidden;
}
.child{
  overflow-y: scroll;
  position:absolute;
  top:0; right:-18px; left:0; bottom:-18px;
}

img {
  width: 318px; 
  vertical-align:bottom;
}

Check out the demo page

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

Unveil as you scroll - ScrollMagic

I am working on a skill chart that dynamically fills when the document loads. I am exploring the possibility of using ScrollMagic to animate the chart filling as the user scrolls down. After trying various combinations of classes and pseudo-classes in the ...

I want to set a single background image for all slides in fullPage.js. How can I achieve this

I am attempting to replicate a similar effect as shown in this example: The demonstration above utilizes the fullPage.js plugin. You may have noticed how the background image remains consistent while navigating through different sections. I have searched ...

Leveraging custom properties in HTML elements with JavaScript

I am in the process of creating a straightforward battleships game that utilizes a 10x10 table as the playing grid. My goal is to make it easy to adjust the boat length and number of boats, which is why I'm attempting to store data within the HTML obj ...

Adjusting value of one input field according to the content of another text field in an

Just starting out with Angular and I am looking to dynamically change the value of a hidden input field called 'cc_card' based on the first digit entered in another input field called 'cc_number'. For example, if the user enters 5 in &a ...

What could be causing my anchored link to redirect incorrectly?

My navigation bar correctly directs me to the 'work' section, but when I click on ABOUT in the navigation bar, it drops down about 300px above the 'about' h2. I suspect that this issue may be related to positioning and display settings. ...

delivering the user's ID and username from php back to the website

Currently, I am in the process of creating a sign-in feature for my website (index.php): To achieve this, I have designed a form that will submit information to the validation-sign-in.php file for processing: (Encryption will be implemented at a later st ...

javascript/AngularJS - make elements gradually disappear

I need help implementing a fade effect for an icon in the middle of a picture that indicates scrollability to the user. Currently, when the user scrolls, I can hide the icon but would like to add a nice smooth fade-out effect. Here is the HTML code snippe ...

Applying CSS to Align List Items to the Left with Word Wrap

Over at my blog page qavalidation.com, I am faced with an issue related to the alignment of vertical menus with links. When word wrap is applied, there seems to be a misalignment in the left indentation. Can someone provide guidance on the correct CSS sty ...

What is the best way to show the response data in an input text field within an HTML form?

I'm new to working with node.js and I've encountered an issue. I need to display the data of the currently logged in user in input fields. With the following code, I can access the data of the logged-in user and display it on the console: See O ...

Declaring an array that holds Angular components in Angular 11: What's the best approach?

I'm trying to implement a feature in my Angular component where I can create a list that displays content from other components. My current approach involves declaring an array that contains references to different Angular components: import { Compone ...

Changing the size of an iframe and closing it by pressing the ESC

I am developing an application that requires the ability to resize an iframe. When the user clicks the "Full Screen" button, the iframe should expand to occupy the entire screen. The iframe should return to its original size when the user presses the "Es ...

The component's div does not respond to the max-width attribute

I'm currently facing an issue with my chart component in a view. I've been trying to reduce its size using max-width property but it doesn't seem to be working. Below are the relevant code snippets: <div id="chart"> < ...

Troubleshooting a layoutUnit problem with Primefaces southern region

I'm encountering an issue with my PrimeFaces layout. The south layoutUnit is not displaying on the page, and I am unsure why. Below is the code for the page: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/ ...

Is the table scrollable within the tbody tag?

Is it possible to create a table with a scrollbar on the right side using only CSS and without any plugins like jQuery? Additionally, I want the table header to remain fixed in place. What steps do I need to take to achieve this functionality? ...

What seems to be the issue with Collapse.js in Bootstrap 3.3.4 on this page?

I am currently utilizing Bootstrap version 3.3.4. All of my resources are linked relatively and the HTML file is in the correct location for access. I am creating a 'Learn More' button that should display a collapsed unordered list either above ...

Material UI button component does not have the applied class

I'm encountering an issue with applying a CSS class (redirectButton) to a Material UI button. Despite my efforts, the class is not being successfully applied. Below is the HTML code in question: {(isDataAdapterAdmin && !dataSources?.length) & ...

Ensuring the Line Breaks in CSS and JavaScript to Easily Modify the Style

Is there a way to determine when a line will break so I can apply different styles? The design team needs 3 buttons in a grid (3 columns) with specific sizes. They want buttons with content that breaks onto the next line to have a border-radius of 13px, w ...

Having trouble locating HTML elements by their id when typing into a box using cy.get in Cypress with JavaScript

I am a novice in working with cypress and HTML, and I am currently attempting to enter an address in the specified address field. <input type="text" title="Street Address 1" name="billing[street][]" id="billing:street1" value="" class="input-text " ...

Error in Node: JSON parse failure due to invalid token "'<'" and ""<!DOCTYPE ""

Every time I attempt to run node commands or create a new Angular project, I encounter the following error. Node version 20.11.0 NPM version 10.2.4 ...

Modifying an HTML attribute dynamically in D3 by evaluating its existing value

I'm facing a seemingly simple task, but I can't quite crack it. My web page showcases multiple bar graphs, and I'm aiming to create a button that reveals or conceals certain bars. Specifically, when toggled, I want half of the bars to vanish ...