scroll smoothly to a specified vertical position on a webpage using jquery animation

Hello everyone, I am relatively new to JS and currently working on creating a vertical image scroller. I have successfully implemented code to scroll from left to right on LI elements until the center of that LI is reached. However, I now want to apply this functionality to a vertical scroller with 6 divs set up. When a thumb is clicked, I would like the LI's to be scrolled to that specific position. Below is the code I have used for horizontal scrolling:

Check out my code on FIDDLE

$('html, body').animate({                    
scrollLeft: ((thisImg.offset().left)-windowWidth+imagewidth)}, 1300);});

I attempted using scrollToTop but it did not work as expected. If anyone could provide guidance on how to fix this issue, I would greatly appreciate it. Additionally, I am looking to make the crimson list items capture 100% width and height. Any suggestions on how to achieve this would be helpful.

Thank you in advance. PS: I am aiming to achieve a similar effect as seen on this Demo Site

Answer №1

Hello @designerNProgrammer, A great approach to building a website is creating functionality that works even without relying heavily on jQuery or Javascript. You can then use these technologies to enhance those features further. Let's take a look at how vertical window scrolling can be achieved using jQuery:

To start, I would include anchor tags within the #thumbs li's that link to specific id's assigned to the #fullList li's. This simple HTML setup operates without requiring Javascript.

<ul id="thumbs">
    <li><a href="#one">1</a></li>
    <li><a href="#two">2</a></li>
    <li><a href="#three">3</a></li>
    <li><a href="#four">4</a></li>
    <li><a href="#five">5</a></li>
    <li><a href="#six">6</a></li>
</ul>
<ul id="fullList">
    <li id="one">1</li>
    <li id="two">2</li>
    <li id="three">3</li>
    <li id="four">4</li>
    <li id="five">5</li>
    <li id="six">6</li>
</ul>

We can now utilize this basic code structure to add animation effects for window scrolling progressively with jQuery.

$(function() {
    $("#thumbs li a").click(function(e) {   
        e.preventDefault();
        var elId = $(this).attr('href');
        var elPos = $(elId).offset().top;

        $('html, body').animate({                    
            scrollTop: elPos
        }, 1300);
    });
}); 

You can test it out in this JSFiddle: http://jsfiddle.net/k2ape/2/

If you have other responsive design concerns, considering posting a separate question on SO with a clear title for better visibility and responses. Happy coding!

Answer №2

Oh, you were almost there! It's actually scrollTop, not scrollToTop. This code snippet has been tested and works perfectly for me:

jQuery(document).ready(function($) {
    $("#thumbs li").click(function() {
        var windowWidth = jQuery(window).width()/2,
            index = $(this).index(),
            thisImg = $("#fullList li:eq("+index+")"),
            imagewidth = thisImg.width()/2;

    $('html, body').animate({
        scrollTop: thisImg.offset().top
    }, 1300);
});
});

If you include normalize.css and remove the margin from the li element, it should stretch the li to 100%. In my example, I didn't use normalize.css but simply set html and body to have no margin or padding.

Check out the Fiddle here: http://jsfiddle.net/maxinacube/6Y3WX For Normalize.css, visit:

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 mobile button bar on iPhone is shown when scrolling or jumping up to the anchor, but it doesn't actually scroll or jump to the

My goal is to include a back-to-top button that remains fixed at the bottom of the browser window. Surprisingly, when clicked, it successfully scrolls to the top in desktop and Android browsers but fails to do so on iPhones. On an iPhone, clicking the butt ...

Updating JQuery function after window is resized

click here Currently, I am using slick-slider to showcase content in 3 columns by utilizing flexbox. The aim is to have the slider activated only on mobile view. This means that when the screen size shrinks down to mobile view, the 3 column flexbox transf ...

Enhancing the background of a website with the power of CSS

I am looking to create a customized table without the balls/pots in it. The number of rows on the y-axis will vary depending on the number of names I have, and can be more or less. The values on the x-axis are determined by a parameter included in the URL ...

Showing a table row in jQuery by targeting an HTML5 attribute of a checkbox that is checked

Hey there, imagine having the following snippet of HTML: <input type-checkbox <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2b6b3a6b3ffa6b7a1a6bbb6ef929fbdb6b7befc86b7a1a696a6bda1">[email protected]</a>[i].I ...

A guide to placing tooltips dynamically in highcharts column charts

I am encountering an issue with tooltips in Highcharts column charts. The problem arises when the series fill up my chart, causing the tooltip to be hidden below the series and cut off by the end of the div. You can see an example here: https://i.stack.i ...

The presence of <!--[if lte IE8]>< ![endif]--> on the website is causing problems with the dropdown

Check out my link: Problem #1: Why does "" appear on the site in Internet Explorer? The markup seems correct, so what is causing this to show up? Problem #2: In Internet Explorer, there is an issue with clicking on subnav items above the jQuery slider wh ...

Leverage the power of the General Sibling Selector to easily traverse through deeply nested elements in your

Hi there, I have a query regarding the General Sibling Selector. Let's start by looking at the HTML: <div id="layout-middle"> <div id="Center"> <a class="col Picture1">Title1</a> <a class="col Picture2">Title2< ...

The initial page in jqgrid may show rowobject value as undefined, but subsequent pages will display the correct rowObject values

While working with jqgrid-4.8, I encountered an issue where the rowObject value is coming up as undefined in the first page but values are received in subsequent pages when using rowObject.name. However, if rowObject[0] is used, the values of rowObject are ...

Modify the width measurement from pixels to percentage using JavaScript

Looking for some help with a content slider on my website at this link: . I want to make it responsive so that it adjusts to 100% width. I managed to make it responsive by tweaking some code in the developer tools, but now I'm stuck. The bottom two p ...

Remove Bootstrap-Table's box-shadow on the search bar's delete button

Is there a way to eliminate the box-shadow from the search box upon clicking on it? I am familiar with removing box-shadows from classes in CSS, but I am unsure of how to remove the box-shadow from a specific property like "data-search" within the tag. ...

Truncating text with ellipsis in CSS when wrapping on a nested div structure

Here is a 3-tier nested div tree structure with the outer node having a maximum width where I want the ellipsis wrapping to occur. I've almost achieved the desired result, but the issue arises when the inner nodes are not trimmed to accommodate as mu ...

When using jQuery's $.ajax function, the success parameter with the textStatus is being disregarded without any indication

I'm confused about what's going on here. The AJAX request is being successfully posted and Firebug isn't showing any errors. However, the success function {alert("complete")} never seems to fire. It's as if it's being ignored. I ...

The dictionary of parameters includes a missing value for a non-nullable 'System.Int32' parameter

Currently tackling a problem while working on an MVC 4 application. In my project, I have an entity named 'FormaFarmaceutica' which has only one field - description. Given the single field, the creation and editing views are displayed in a modal ...

Form submission fails to trigger AJAX execution

When a form is submitted, I have the jQuery ajax code below that should be executed: <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> <script> $("form").submit(function() { $.ajax("/myapp/rest/customer/created ...

What is the best way to insert data into a multidirectional array using jquery?

I'm working with a Purchase Table that looks like this: Item QTY Rate Amnt ABC 2 100 200 DEF 1 300 300 When the save Button is clicked, I need to store this information in a multidirectional array as follows: ...

Transforming an image into a geometric shape using html and css - step by step guide

Hey fellow stackoverflow users! I've created a geometric figure using multiple divs for an unconventional website design. However, I'm struggling to adapt images within these divs because the original width is not being maintained. When you view ...

How to pass only the clicked element to the onClick function in React.js

I have several elements with the same className, and I want to add the className active to an element (with the className history-node) when it is clicked, in addition to its current className. However, I am facing an issue where the child elements of tha ...

What is the best way to iterate through each input in every row and add it to the database?

I am facing a challenge with a form that has an indefinite number of input fields. . Upon clicking submit, I need to insert each row into the database with unique IDs. For example, the first row should have an ID of 100, the second row an ID of 101, and ...

Is there a way to stack overlapping divs in a layout that resembles a pile of papers?

I am trying to create a layered effect with multiple divs, where each div is positioned slightly up and to the left of the previous one, giving the appearance of a stack of papers. I have experimented with absolute positioning and z-index, but I'm hav ...

Tips for using various versions of jQuery at the same time

Despite searching on Stack Overflow, none of the answers provided seem to work for my issue. My requirement is to utilize two separate versions of jQuery. Below is the code snippet: I first link to the initial version and use the following code: <sc ...