Looking for assistance with implementing mouseover scrolling on an unordered list

I am attempting to create a scrolling effect on my UL element when the mouse hovers over it, but I am having trouble getting it to work. Here is the link to my fiddle: http://jsfiddle.net/MisterStorm/4ja9apqz/3/ and here is my code:

$(document).ready(function () {
  $('#next').hover( function () {
    scrollCallback = setInterval(function () {
      $('#divContainer ul').animate({
        'scrollLeft': $('#divContainer ul').offset().left
      }, 500);
      console.log($('#divContainer ul').offset().left);
    }, 50);
  }, function () {
    clearInterval(scrollCallback);
  });
});
#divContainer {
  width:300px;
  overflow:hidden;
  border:solid 1px #000;
  position: relative;
  height:20px;
}
#divContainer ul {
  list-style-type: none;
  position:absolute;
  top:0px;
  left:0px;
  margin:0;
  padding:0;
}
#divContainer ul li {
  width: 20px;
  height: 20px;
  line-height: 20px;
  font-size: 13px;
  display: inline;
  margin-left: 3px;
  margin-right: 3px;
  float: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="divContainer">
  <ul>
    <li><a href="#">1</a></li> 
    <li><a href="#">2</a></li> 
    <li><a href="#">3</a></li> 
    <li><a href="#">4</a></li> 
    <li><a href="#">5</a></li> 
    <li><a href="#">6</a></li> 
    <li><a href="#">7</a></li> 
    <li><a href="#">8</a></li> 
    <li><a href="#">9</a></li> 
    <li><a href="#">10</a></li> 
    <li><a href="#">11</a></li> 
    <li><a href="#">12</a></li> 
  </ul>
</div>
<div id="next">next</div>

Any assistance would be greatly appreciated! Thank you!

Answer №1

Is this the solution you're looking for?

http://jsfiddle.net/8dk2hajq/12/

I had to use a different approach because I couldn't grasp your method.

My solution involves retrieving the left offset using the following code:

leftOffset = $('#contentWrapper ul').css('left');

Then, I adjust the left offset by -5px every 50ms and reset it to 0 when the hover action ceases.

Answer №2

Utilize css :hover

#containerDiv {
    width:300px;
    overflow:hidden;
    border:solid 1px #000;
    position: relative;
    height:20px
}
#containerDiv ul {
    list-style-type: none;
    position:absolute;
    top:0px;
    left:0px;
    margin:0;
    padding:0;
    -webkit-transition: transform 6s ease;
    -moz-transition: transform 6s ease;
    transition: transform 6s ease
}
#containerDiv ul li {
    width: 20px;
    height: 20px;
    line-height: 20px;
    font-size: 13px;
    display: inline;
    margin-left: 3px;
    margin-right: 3px;
    float: left
}
#containerDiv ul:hover{
   -webkit-transform: translateX(-100%);
   -moz-transform: translateX(-100%);
   transform: translateX(-100%)
}
<div id="containerDiv">
    <ul>
        <li><a href="#">1</a></li> 
        <li><a href="#">2</a></li> 
        <li><a href="#">3</a></li> 
        <li><a href="#">4</a></li> 
        <li><a href="#">5</a></li> 
        <li><a href="#">6</a></li> 
        <li><a href="#">7</a></li> 
        <li><a href="#">8</a></li> 
        <li><a href="#">9</a></li> 
        <li><a href="#">10</a></li> 
        <li><a href="#">11</a></li> 
        <li><a href="#">12</a></li> 
    </ul>
</div>
<div id="nextBtn">next</div>

Answer №3

I managed to solve my issue by setting both the ul and li elements' position to absolute, allowing them to move freely.

Below is the updated JavaScript code snippet from my project:

thisObj.find('#sliderleft').hover(function () {
    scrollCalbackRight = setInterval(function () {
    var pos = optionList.holderPagerList.css('left').replace('px', '') * 1;
    if (pos < 0){
        optionList.holderPagerList.css('left', (pos * 1) + optionList.scrollerDistance);
    }
}, 50);
}, function () {
    clearInterval(scrollCalbackRight);
});

This code snippet also accounts for when the position is at the start.

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

When implementing the Slick Carousel with autoplay within an Isotope grid, the height of the image slides may occasionally reduce to 1 pixel when applying filters

I've been struggling with this issue for more than a day now, and despite searching through similar posts and trying their solutions, I still haven't been able to resolve it. Here's an example of the problem: https://jsfiddle.net/Aorus/1c4x ...

Using the <blink> tag on Internet Explorer

Unfortunately, Internet Explorer does not support the <blink> tag or the text-decoration:blink; style in CSS. Are there any alternative methods for creating blinking text in IE? ...

Unable to fix position: sticky issue following Angular 15 update

Following the angular material update to version 15, many of us experienced issues with CSS changes breaking. This also affected legacy code that included sticky headers. <div class="row"> <div class="col-12"> <di ...

Low-quality CSS Gradient Design

Hey everyone, Feel free to take a look at my preloader on this link: (Hit ESC as soon as the page loads to pause the website and focus on the loader) Upon closer inspection, you'll notice that there is an issue with the quality of the background l ...

Enable content to be displayed when hovering over it with a mouse in

As a newcomer to JavaScript, I am eager to learn how to add an interactive feature to my menu item. I want to create an info box that pops up when the menu item is clicked, and can also be displayed when hovering over it. Below is the script I have so far, ...

Ways to dynamically update a div with data from a JSON response

I'm currently in the process of developing a search platform. I have three static divs on the search results page that display certain content, all containing similar code. For example: <div id="result" class="card"> <img src="hello.png" ...

The subdirectory containing the Rails assets is causing loading issues in the production environment

My event CSS is currently located in app/assets/stylesheets/pages/events.scss. Surprisingly, everything looks perfect in development mode with all the assets loading properly. However, the real issue arises when I switch to production as it seems like thes ...

What are the benefits of using CakePhp AJAX helper over jQuery?

Recently, I've delved into the world of cakePHP and noticed the presence of an AJAX Helper within it. My dilemma is straightforward: should I opt for this helper over continuing to use AJAX with jQuery? Are there specific adjustments required for exi ...

Manipulate an image with PHP and jQuery to rotate it, then store the edited image in a specified destination directory

This is not just a question, but an opportunity to share knowledge with many like-minded individuals. After spending hours working on rotating images dynamically using PHP and jQuery, I have come up with a solution that I believe will benefit others in the ...

Padding for the doughnut chart in Chart.js canvas

My current setup includes a canvas featuring a doughnut chart created with chart.js enclosed in a div element that displays like this: The canvas has a red background to enhance visibility. Currently, the chart is centered within the canvas with additiona ...

Unable to obtain accurate data returns

What is the issue here: $('#btnSend').click(function() { var msg = $('#txtar').val(); // textarea alert (msg); // this works fine, for instance 'xyz' $.ajax( { type: "POST", url: "sendD ...

Transforming jQuery Object into a String after making an AJAX request

If I were to submit a form with some text in the value of user_input, let's say "I am free," through AJAX, and it comes back to me as a string. Once it becomes an Object, how could I convert it back into a string format? Thanks, <!DOCTYPE HTML> ...

The alignment issue persists when attempting to set 'relative' on the parent and 'absolute' on the inner element in a Bootstrap column for bottom alignment

Here is a snippet of my basic HTML code: <div class = "row"> <div class = "col-xs-8"> <p>long long long long text that generates a taller column</p> </div> <div class = "col-xs-4"> <p> ...

Determine the total number of selected items in MagicSuggest when it loses focus

I have been working with MagicSuggest and I am currently facing an issue where I need to retrieve the length of selections on a blur event. Interestingly, my code functions perfectly fine when a new selection is added using the ENTER key, but it fails when ...

A guide on applying color from an API response to the border-color property in an Angular application

When I fetch categoryColor from the API Response, I set border-left: 3px solid {{element.categoryColor}} in inline style. Everything is functioning correctly with no development issues; however, in Visual Studio, the file name appears red as shown in the i ...

Unable to slide the form upward

I have set up 2 forms on the same page for sliding effect. HTML - first form <form action="index.php" method="post" id="reg-1" enctype="multipart/form-data"> <input type="text" name="userName" placeholder="Username" maxlength="50"> < ...

Tips for deleting "Category: [category name]" in Wordpress

I have attempted various solutions found in previous threads, but none of them seem to work for me. Here is a screenshot I am looking to remove the text from the header in the category and only display [category name]. Can anyone assist me with this, ple ...

I'm looking for help on creating a three-column table using javascript/jquery. The table should display Product, Price, and Discount (which is calculated at 20%). Can anyone

Check out this code snippet. var items = ["Laptop", "Tablet", "Smartphone", "Headphones", "Camera"]; var costs = [599.99, 299.99, 799.99, 149.99, 499.99]; displayItems = ""; totalCost = 0; for (var j = 0; j < items.length; j++) { displayItems += " ...

Send the Entity's identification number to the Controller using Ajax

What is the most effective method for transmitting an object's identifier via ajax to the Server / Controller? For example, when we are on the page "book/edit/mybookname" and want to update the book entity "mybookname" using ajax. I have considered v ...

Is it possible to customize the arrow on a drop-down menu?

Here is an example of the page I am facing issues with: If you look at the right side of the bottom bar, you will notice a selection option for different themes. I have been attempting to replace the down arrow with an image. Below is the code I have used ...