The overflow:hidden property is causing issues with the functionality of the

My slider moves back and forth between divs when a link is clicked. It works perfectly until I add overflow: hidden, then everything falls apart!

I have put together a sample demonstration for anyone to explore. You can see that it functions, but if you try adding overflow: hidden to the .clickWrapper div to conceal the sliding divs, it will malfunction. When you click on one of the options, it will skip past other divs.

http://jsfiddle.net/z67tZ/

$(function() {
    $(".clickIt a").click(function() {
        var linked = $(this).attr("href");
        var pos = $(linked).position();
        $(".clickSlider").stop().animate({
            left: -pos.left,
        }, 500);
    });
});
<div id="wrapper">
    <div class="clickIt">
        <a href="#one">one</a>
        <a href="#two">two</a>
        <a href="#three">three</a>
    </div>
    <div class="clickWrapper">
        <div class="clickSlider">
            <div id="one">one</div>
            <div id="two">two</div>
            <div id="three">three</div>
        </div>
    </div>
</div>
#wrapper {margin: 0 auto; width: 200px;  }
.clickWrapper {background:red; position:relative; overflow:hidden; background-color: #CCC; height: 200px; }
.clickSlider { position: relative; width: 600px; }
#one, #two, #three { float: left; width: 200px; }

Answer №1

Here's a solution to prevent the default hashchange behavior:

$(".clickIt a").click( function (e) {
    e.preventDefault();
    var linked = $(this).attr("href"),
        pos = $(linked).position().left;

    $(".clickSlider").stop().animate({left: -pos}, 500 );
});

Check out this FIDDLE for a demo.

It seems like there might be an issue with how browsers handle anchor links with hashes. When a hash is clicked, the browser tries to scroll to an element with a matching ID, which can mess up positioning in certain cases. To avoid this, adding preventDefault() or return false when using hash values as IDs can help prevent unexpected bugs.

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

Unchanging Dive Field

I'm having trouble understanding why this field isn't updating with the correct number. It seems that any value less than 1 is being rounded in the alert() function. I believe all numbers are simply getting rounded down, but I'm unsure of an ...

Ways to extract information from JSON files

Currently, I am working on a script to extract viewer count and follower count data from Twitch. While I have successfully retrieved the viewer count information, I am encountering issues with extracting the follower count. The essential information can be ...

Easily submit both FormData and a string in a single function call

I am trying to send data from a form (including a file input and string input) via ajax to an ASP.NET Function. When sending only files, I use the following code: function readURL() { var input = document.getElementById("fileUpload"); var files ...

What is the best way to position the navbar above all the other text content?

I'm experiencing an issue where the navbar is not appearing on top of all the text/buttons. How can I adjust it so that the navbar appears on top? Thank you. https://i.sstatic.net/59M2l.png [Codepen](https://codepen.io/turbo0/pen/NWBMNQr) ...

Creating text on a background image that does not adapt to different screen sizes is

How can I ensure that the text I write on an image stays within the image boundaries on small screen devices for full responsiveness? I've tried setting the image as the background of my div element, but the text overflows. Here is the code I used: ...

Button with small width containing an icon and text

I am trying to create a simple HTML button that has an icon on top and a description below it. <div class="mybtn"> <div><img src="someimage.png"></div> <div>Button description</div> </div> My question is, ...

I prefer to disable the toggle feature if the target remains unchanged

My goal is to create a step-by-step ordering system using the data-id="x" attribute to determine which content should be visible when selected. I want to make sure that if two elements have the same data-id, clicking on one will deselect the other. Any su ...

When implementing afui and jQuery on PhoneGap, an error was encountered: "TypeError: Cannot read property 'touchLayer' of object function (selector, context)"

While working on deploying a web application using phonegap with afui (Intel Appframework UI) for Android, I encountered an issue when testing it in the android emulator. The debug console displayed the following error right after launching the app: Uncau ...

Ways to troubleshoot the MudTabPanel during scrolling

I am trying to create a fixed text at the top of a scrollable MudTab. As I scroll down, I want the text to remain visible. Here is an example of what I have done: <MudTabs Position="Position.Top" PanelClass="pt-2" Style="hei ...

Troubleshooting Problems with display:table and display:table-cell Alignment in Internet Explorer Versions 7 and 9

I have been struggling with the layout of a website that contains multiple columns. After extensive work, I believed I had everything properly aligned. Adobe BrowserLab seemed to confirm this, aside from various issues in IE6 and IE7 which I decided to ove ...

A guide on retrieving JSON data from an AJAX request using Angular UI Bootstrap

Greetings Friends, I have experience with traditional JavaScript and a little bit of JQuery. Now, for my new JAVA Web Based project, we need to utilize the latest technology to quickly build dynamic web pages. I don't have time to test all the widget/ ...

Searching for a specific element using a CSS selector

Looking for a way to find an element using a CSS selector in Chrome, I followed these steps: Right-click on the object Select "inspect" Right-click on the highlighted area in Chrome developer tools Choose "copy" Click on "copy selector" This is the cod ...

Ways to position a DIV on the right side of the screen using percentage margin to adjust its distance from the edge as the browser is resized

Allow me to provide a clearer explanation here: Imagine an element placed on a webpage with a margin of 10% from the left side. When the size of the page is adjusted, the margin on the left decreases accordingly - for example, 10% from 1400px - 140px, and ...

Display a kendo window in Kendo Scheduler MVC by double clicking on a schedule

How can I set up my scheduler to open a kendo-window with a double click on a cell instead of a single click? Currently, the kendo-window pops up every time I click on the scheduler, even when navigating between month and day views... Any suggestions or f ...

The card-img-overlay in Bootstrap 5 seamlessly integrates into the following div container

Currently, I am in the process of creating a painting website layout for my CS50 homepage. However, there seems to be an issue with the background-color: rgba(0,0,0,0.7); not aligning correctly with the images within the card-group. <div class="car ...

issue with lists not displaying correct heights

Could jquery be used to calculate the height of ordered lists? I seem to be receiving a NULL value. js var tallestOl = $('ol.mostpopular').height(); console.log(tallestOl); html <ol class="mostpopular"> <li class="headin ...

What could be wrong with my hover command not functioning properly?

I've been attempting to create a dropdown menu using some basic CSS, but I can't figure out why the dropdown isn't functioning. I've tried everything I can think of. Below, I've included the CSS and HTML code for reference: < ...

jQuery animation not executing as expected due to transition issues

I'm looking to create a cool effect where the previous quote disappears and a new one appears when I click on a button, all with a smooth transition effect. In my attempt below using jQuery .animate and opacity property, I'm struggling to make i ...

JQuery-UI alert for Checkbox Selections

Clicking the link will trigger a dialog box to pop up. When the "Download Later" button is clicked, it will toggle the checked variable of the button and then close the dialog. I am just starting out with Javascript and JQuery, so I'm still trying to ...

The CSS transition fails to animate with dynamically loaded Ajax content

My journey into the world of CSS animations is relatively new, with some previous experience in JQuery. However, I encountered a problem with choppy animations on smaller devices like my iPad, nook, and android phone. To address this issue, I decided to us ...