Turn off horizontal scrolling on your mobile site

I am working on a mobile webpage with a Facebook-like side menu button, but I'm having trouble disabling horizontal scrolling using CSS overflow-x:hidden. Here is the code I have so far:

<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>



    <script> 
        $(document).ready(function(){                       
          $("button").click(function(){ 

              var win = $("#right-side");
              var position = win.position();
              //alert( "left: " + position.left + ", top: " + position.top ); 

              if(position.left < 100)
              {
                  $("#right-side").animate({left:'250px'});
              }else{
                  $("#right-side").animate({left:'0px'});
              }

          });
        });
    </script> 

    <style>
        body{overflow-x: hidden;font-family: sans-serif;}

        #right-side{
            background:#BFC7D8;;
            left:0;
            top:0;
            height:100%;
            width:100%;
            position:absolute;

        }
        #left-menu
        {
            background:#323949;
            left:0;
            top:0;
            height:100%;
            width:250px;
            position:absolute;
        }

        #navigation { font-size:20px; width:250px; padding-top:100px; }
        #navigation ul { margin:0px; padding:0px; }
        #navigation li { list-style: none; }



        ul.top-level li > a {
          display: block;
          border-bottom: 1px solid rgba(0,0,0, 0.1);
          border-top: 1px solid rgba(255, 255, 255, 0.1);
          padding: 15px;
          text-shadow: 0 1px 0 #000;
          text-decoration: none;
          color: #ccc;
          text-indent: 20px;
        }

        #toolbar
        {
            width:100%;
            height:50px;
            background:#00F;    
        }


    </style>





<div id="left-menu">
    <div id="navigation">
    <ul class="top-level">
        <li><a href="#">Home</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Contact</a></li>
        <li><a href="#">FAQ</a></li>
        <li><a href="#">News</a></li>
    </ul>
</div>  
</div>




<div id="right-side">

    <div id="toolbar">
        <button>Menu</button>
    </div>

    <h1>This is a test</h1>
</div>

Answer №1

I've placed your code in a fiddle, but unfortunately, I was unable to include the 'zoom' meta tag in the head section, making it difficult to test on my phone. http://jsfiddle.net/Pevara/Ku5nY/1/show/

However, it seems to be working perfectly on desktop with no scrollbars.

In an attempt to improve it, I made the following additions to your css:

body{
  overflow-x:hidden;
  font-family: sans-serif;   
  /* added: */
  max-width: 100%; 
  height: 100%;
}

Although uncertain if this change will have any effect, it's definitely worth giving it a shot...

Answer №2

Hey there, David!

Are you familiar with this amazing resource:

If not, I suggest taking a look at this link to see if it can help enhance your code:

http://jsfiddle.net/tzDjA/

In the sample provided, there are 3 essential functions:

$('#trigger').click( function() {
    if ($('#popout').hasClass('hidden')) {
        $('#popout').removeClass('hidden');
        showPopout();
    }
    else {
        $('#popout').addClass('hidden');
        hidePopout();
    }
});

function showPopout() {
    $('#popout').animate({
        left: 0
    }, 'slow', function () {
        $('#trigger span').html('Close');  //adjust trigger text after animation
    });
}

function hidePopout() {
    $('#popout').animate({
        left: -40
    }, 'slow', function () {
        $('#trigger span').html('Show');  //modify trigger text after animation
    });
}

Answer №3

I encountered a problem where the menu was hidden behind my content and I had to push the content to the left in order to reveal it, similar to the style of Facebook's menu.

To achieve the sliding effect, I used 'absolute' positioning to take the content out of the document flow. Despite setting overflow to hidden and adjusting x & y overflows on multiple elements, the issue persisted. Even with width:100% on body, the problem still occurred.

Eventually, I found that changing the content layer to 'relative' while sliding it across, and then reducing the height of the content to match the window's height when the menu was open, solved the issue effectively across different devices.

If you are facing a similar problem, try this solution. Good luck!
Thank you, Dave

Answer №4

If you want your modal to function properly, make sure to set the height attribute. Failing to do so will result in it not working at all. To achieve this, set the dialog's height to 100% and use overflow:hidden. Next, adjust the content by setting its position to absolute, top:0, bottom:0, left:0, right:0, margin:auto, and defining a specific height (for example, 250px for a login modal). While this may seem counterintuitive - possibly due to a CSS issue, it does work seamlessly across different browsers and platforms (iPhone compatibility has not been verified).

<div class="modal-dialog" style="height:100%;overflow:hidden"><div class="modal-content" style="position:absolute;margin:auto;top:0;bottom:0;left:0;right:0;height:250px;">

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

Encountering the error "TypeError: Router.use() is expecting a middleware function but received undefined" when attempting to import a router from another file

Trying to configure a router for user paths and exporting it for use in index.js is causing an error. The following error message is displayed: C:\Users\yugi\OneDrive\Documents\codes\NodeJs\API_nodejs\node_modules&bs ...

Oops! It seems like there's a problem with reading the 'strEmail' property of undefined. Do you have any ideas on how to fix this issue

Currently, I am working with Express.js to create a straightforward login post request. Here is the code snippet: app.post("/login", (req, res) => { res.send( { isUserRegistered: userLogin(req.body.strEmail, req.body.strPassword), ...

Dealing with ETIMEDOUT error in Express.js

My Node.js/Express.js application interfaces with an FTP server, but crashes when the server is offline due to handling issues with the ETIMEDOUT exception. I am using the jsftp module for FTP. Here's the problematic part of my code: router.post("/" ...

Invoking the callback function within the containing scope in Typescript

I am facing an issue with my Angular component where I have a class that includes common services and functions. While passing some functions as callbacks, the scope is getting lost during execution. Let me demonstrate the problem through the code below: @ ...

What is the proper way to format lengthy lines in Twig templates while adhering to coding standards?

In my templates, I have the following code snippet for generating routes with parameters: <a class="btn btn-block btn-abcd" href="{{ path( 'auth.login', { 'type': constant('User::TYPE_CANDIDATE'), & ...

What is the process for assigning a PHP function's return value to a JavaScript variable?

Is there a way to retrieve a value from a PHP function and assign it to a JavaScript variable? As shown in the following example: PHP: // Destination folder for downloaded files $date = date('m.d.y'); mkdir("uploads/" . $date, 0777, true); $ ...

Is there a way to export ng-grid data to a PDF file using jsPDF or a

Hey there, I'm currently working with an AngularJS ng-grid and I'm facing a challenge. I've been looking for a way to use jsPDF to render it as a PDF. While I've had success rendering PDFs of simple text and complicated SVGs through th ...

Struggling with expanding a Javascript object? Let us assist you!

Imagine having a JavaScript object... app.widget = {} (function (W) { W.superFunction = function() { console.log("Hey!"); } ...other functions... })(app.widget) Now, let's create a clone of this object... app.specialWidget ...

Updated: "Adjust the preserveAspectRatio attribute in the SVG element within the Lottie player"

I have successfully incorporated a lottie json file using the lottie player. Unfortunately, I do not have access to the SVG file itself, only the json file. My goal is to modify the preserveaspectratio attribute of the SVG to xMinYMax meet instead of xMi ...

JavaScript can be used to arrange a table in both ascending and descending order by simply clicking on the header

window.addEventListener('DOMContentLoaded', () => { let dir = "dsc"; th = document.getElementsByTagName('th'); for(let c=0; c < th.length; c++){ th[c].addEventListener('click',item(c)); } ...

What is the best way to duplicate all elements and their contents between two specified elements, and store them in a temporary

context = document.createElement("span"); start_element = my_start_element; end_element = my_end_element; // I need to find a way to iterate through a series of elements between start and end [start_element .. end_element].forEach(function(current_element ...

Node-RED experiences crashes while attempting to make HTTP requests to access a web service

I'm currently facing a challenge in creating a node that can make web service calls. The tools I'm using are: Node-RED version: v0.17.5 Node.js version: v8.4.0 An exception is being thrown, and here's the error message: node-red_1 ...

How come the array's length is not appearing on the browser screen?

Code: initialize: function() { this.todos = [ {id: 100, text: 'Rich'}, {id: 200, text: 'Dave'} ]; }, activeTodos: function() { this.todos = this.todos.length(function() { return this.todos; }); ...

Best practices for starting and stopping trace spans in express / nodejs applications

Currently, I am conducting an experiment with utilizing the Opentelemetry.js nodejs/express library and attempting to refactor the reviews application from the bookinfo Example found in Istio. I followed the tracer configurations laid out in the sample: ...

Ways to Create a Webpage with a Single Color Palette

Struggling to update the background color on my webpage and hitting a wall. Even with content, the black background only extends as far as the content does. I've experimented with extending the content, using the body selector, and universal selector ...

What are the consequences of excluding a callback in ReactJs that may lead to dysfunctionality?

<button onClick = { () => this.restart()}>Restart</button> While going through a ReactJs tutorial, I encountered a game page which featured a restart button with the code snippet mentioned above. However, when I tried to replace it with the ...

Efficiently verifying elements within an array

I have a roster of students categorized based on their activity status - some are active, while others are inactive. var allActive = [{id: 1, active: true}, {id: 2, active: true}, , {id: 3, active: true}]; var someNot = [{id: 4, active: true}, {id: 5, act ...

The conflict between CSS link types and image links causing disruption

I am currently designing my own Tumblr theme and have encountered an issue that I cannot resolve due to lack of expertise. I was hoping someone with more knowledge could assist me. Essentially, I have implemented a feature where hovering over a link chang ...

TypeORM ensures that sensitive information, such as passwords, is never returned from the database when retrieving a user

I developed a REST API using NestJs and TypeORM, focusing on my user entity: @Entity('User') export class User extends BaseEntity { @PrimaryGeneratedColumn() public id: number; @Column({ unique: true }) public username: string; publi ...

The minimum and maximum validation functions are triggered when I am not utilizing array controls, but they do not seem to work when I use array controls

Take a look at the stack blitz example where min and max validation is triggered: https://stackblitz.com/edit/angular-mat-form-field-icrmfw However, in the following stack blitz with an array of the same controls, the validation does not seem to be worki ...