poor scrolling performance when transitioning focus between elements in the interface

Looking for some assistance? Check out this codepen.

I'm currently working on a search bar widget that allows navigation from the input field to the search results, displayed as a series of divs. The navigation is controlled via jquery, where the focus shifts between div elements triggered by arrow key inputs.

The divs are contained within a parent div.s_dropdwn with specified styling attributes like max-height:100px and overflow-y:scroll.

While the navigation functionality works smoothly, there is an issue with scrolling behavior. Upon pressing the down arrow key starting at the input, the focused div disappears due to scrolling movement.

My aim is to achieve seamless navigation where you can move down to the fifth div before triggering any scrolling actions.

$('.move').keydown(function(e){

if (e.keyCode == 40) {


$(".move:focus").attr('tabindex','-1')
$(".move:focus").next().attr('tabindex','0').focus();

}

if (e.keyCode == 38) {    

    $(".move:focus").attr('tabindex','-1')
$(".move:focus").prev().attr('tabindex','0').focus();

}


});


$('.s_dropdwn > .move.first').keydown(function(e){

if (e.keyCode == 38) { 

    $(".move.first:focus").attr('tabindex','-1')
$("input").focus()
}


});


$("input").keydown(function(e){

if(e.keyCode === 40){
$('.s_dropdwn > :first-child').attr('tabindex','0').focus();
}


});

*{
margin:0px;
padding:0px;
}

div.s_container{

width:40%;
position:relative;
margin:auto;/* horizontally center the div*/
margin-top:10%;                        /*margin in percent based on width of container .parent1*/
background-color:#becee8;
line-height:62px;

}



.con_inpt{
position:relative;
display:inline-block;
vertical-align:middle;
line-height:normal;

border:white solid 1px;
margin-left:5px;
margin-bottom:3px;
width: 200px;

}

.s_dropdwn{

position:absolute;
top:100%;
max-height:150px;
width:100%;
background-color:#becee8;
margin-top:2px;
overflow-y:scroll;
}

.s_dropdwn > div:focus{

outline:none;
background-color:black;
}

.s_dropdwn > div{
display:block;
width:100%;

height:30px;
line-height:30px;

border-top: solid 1px white;
font-size:0.5rem;
font-family: Arial,sans-serif;
font-weight:600;
color:white;

box-sizing: border-box;        
    padding-left: 10px;           /*because of box-sizing padding will be inside the div not outside*/
cursor:pointer;
transition: background-color 0.4s;
}


input#search{

display:inline-block;
border:none;
background:none;
outline:none;

width:80%;
padding:7px 3px;
font-size:0.8rem;
font-weight:600;
color:white;
}


input#search::placeholder{

font-size:0.8rem;
font-weight:600;
color:white

}
<!DOCTYPE html>
<!-- saved from url=(0048)file:///C:/Users/jvalica/Downloads/d3%20(2).html -->
<html lang="en">
<head>
    <meta charset="utf-8>

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>


</head>

<body>


<div id="parent1" style="position:relative; width:80%;margin:auto; height:300px;border: 1px solid #e4f2f5; background-color: #e4f2f5;">



<div class="s_container">
<div class="con_inpt">
<input id="search" type="text" class="" name="" placeholder="Search" autocomplete="off">


<div class="s_dropdwn">

<div class="move first">
AAA
</div>
<div class="move">
BBB
</div>
<div class="move">
CCC
</div>
<div class="move">
DDD
</div>
<div class="move">
EEE
</div>
<div class="move">
FFF
</div>
<div class="move">
GGG
</div>
<div class="move">
HHH
</div>
</div>
</div>
</div>
</div>
</body>
</html>

Answer №1

After some investigation, I was able to identify the root cause of the issue. For anyone facing a similar problem down the line, here is what worked for me. The issue stemmed from the browser's scroll behavior conflicting with my own scroll behavior triggered by focus changes. The fix involved disabling the browser's scroll behavior. You can achieve this by using the code snippet below:

window.addEventListener("keydown", function(e) {
// space and arrow keys
if([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
e.preventDefault();
}
}, false);

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

The $watch() function seems to be failing to properly refresh the $scope

Within my controller, I have a function $scope.remove() that triggers a request to the usercart, which then calls an API. The JSON response from the API includes an object with an array of cart items. Despite using an ng-repeat in the HTML to iterate thro ...

Semantic UI (React): Transforming vertical menu into horizontal layout for mobile responsiveness

I have implemented a vertical menu using Semantic UI React. Here is the structure of my menu: <Grid> <Grid.Column mobile={16} tablet={5} computer={5}> <div className='ui secondary pointing menu vertical compact inherit&apos ...

How can I change the padding that is being inherited from "@ .MuiAlert-icon"?

Is there a method to override the padding inherited from "@ .MuiAlert-icon"? The Chrome inspector indicates that the current padding is set to 7px. .MuiAlert-icon { display: flex; opacity: 0.9; padding: 7px 0; font-size: 22px; ...

Displaying variables in JavaScript HTML

<script type ="text/javascript"> var current = 0; </script> <h3 style={{marginTop: '10', textAlign: 'center'}}><b>Current Status: <script type="text/javascript">document.write(cur ...

Retrieve information from MySQL database with jQuery upon button click

After fetching multiple data rows from a MySQL database and displaying them in a table, I want to include a button in the last column of each row. The unique ID for each button will be retrieved from the database. When a user clicks on any of these buttons ...

Tips for positioning a canvas and a div element next to each other on a webpage

I have a canvas and a div element that I need to split in a 60% to 40% ratio. However, no matter what changes I make to the display settings, the div is always displayed before the canvas. The Div element contains buttons with color-changing properties fo ...

I am having trouble retrieving any data when using jQuery to post JSON data

I am struggling with some HTML and JavaScript code. Here is what I have: <html> <head> </head> <body> <script src="view/backoffice/assets/plugins/jquery/jquery-1.11.1.min.js" type="text/javascript"></sc ...

Verify if spacebar is pressed and then use jQuery to add a hashtag with multi-language support

I am attempting to use jQuery to add a hashtag (#) after the user types and presses space. I have created a demonstration on CodePen. In this demo, when you type something like (how are you), the JavaScript code will change it to (#how #are #you). To ach ...

What is the correct way to implement ::v-deep(<inner-selector>) in a Vue component?

I am attempting to assign an existing style class to a child element that will be loaded using the v-html directive. Since /deep/ and >>> are no longer supported, I have tried using ::v-deep in Vue. <template> <div v-else v-html="chi ...

Angular not updating the values in real time

First and foremost, I am utilizing socket.io to emit data. Data is emitted upon connection (so the website does not appear blank) as well as when certain events occur. Upon initial connection or page refresh, everything updates and functions smoothly. Howe ...

Unexpected behavior observed when animating CSS transform in IE

I am currently facing an issue with a CSS animation on my website that animates an arrow back and forth. The animation is working perfectly on all browsers except for Internet Explorer, where there seems to be a glitch causing the Y position of the arrow t ...

Maintain the background div while scrolling horizontally

I am facing an issue with my wide bar chart where I use iScroll to enable horizontal scrolling. Behind the bar chart, there are horizontal lines that should move along in the background as the user scrolls horizontally. The challenge is to keep the lines v ...

Tips for utilizing Jquery webcam on mobile devices like Android and iPhone

Currently, I am in the process of developing mobile web applications utilizing jquery mobile and HTML 5. I am working on a feature that requires access to the camera. In order to achieve this functionality, I have integrated the following plugin: While ...

Using jQuery to eliminate repetitive line dividers in a document

Is there a way to remove duplicate dividers in my bootstrap pull-down menu (MVC5 website) that appear stacked up due to user permissions? Here is an example of the issue: <li class="divider"></li> <li>@Html.RouteLink("option1", "Route1") ...

Executing function with ng-click in AngularJS only on the second click of the button

Currently, I am studying AngularJS and working on an exercise that involves using the ng-click function. Strangely, I am only able to view the result after clicking upload for the second time. I am trying to display my json content but it's not workin ...

Tips for accessing a URL with a specific value in the HTML file

This form submits a URL in this format: <form action="search.php?search=" method="GET" id="search-form"> <input id="search-text" name="search" type="text" maxlength="30" placeholder="type keyword"/> <button type="submit" name ...

Convert the information within the data variable into a well-formatted JSON structure

I am attempting to transmit JSON information to the server utilizing the following URL: . The data variable is being populated with valid JSON containing properties such as name, email, and URLs. ...

No output is displayed in the absence of errors. The program is functioning correctly

app.js angular.module('app', ['ionic', 'ui.router']) .config(('$urlRouterProvider', '$stateProvider', function($urlRouterProvider,$stateProvider){ $urlRouterProvider.otherwise('/'); $sta ...

The Bootstrap modal will not appear when the parent container's position is fixed

I am having an issue with a bootstrap modal that I am using as an instruction guide. My goal is to keep it fixed at the top-right corner of the viewport, so I tried using the fixed position. However, when I do this, the modal turns grey. On the other han ...

Utilizing HTML5 to automatically refresh the page upon a change in geolocation

I have a web application built with AngularJS. One of the functionalities is activated only when the user is in close proximity to specific locations. To make this work, I can constantly refresh the page every 5 seconds and carry out calculations to dete ...