Creating Functional Tabs Using CSS and JavaScript

I've been experimenting with this code snippet, trying to get it to work better. It's still a work in progress as I'm new to this and have only customized it for my phone so far. The issue can be seen by clicking on the Projects and Today tabs.

Within the div (#data-container), there are two child divs (.project, .today) that I want to display side by side like tabs. When clicked, they should swipe and show their respective content. While I have managed to make it functional, I am encountering two problems.

Functionality Explanation - The #data-container has white-space: nowrap set (to keep child divs side by side and enable sliding functionality) while its child divs (.project and .today) have widths set to 100% and are positioned as inline-block.

Issues Encountered

  1. The data-container is required to allow vertical scrolling and wrap text around the selected div. However, using white-space: nowrap causes text overflow. Attempts to resolve this using word-wrap: break-word have been unsuccessful. Although setting display: hidden resolves the issue, it interferes with the swiping functionality desired.

  2. An unexpected complication arises when setting #data-container to overflow-y: scroll, causing the divs to scroll horizontally, disrupting the entire system.

A solution is needed to enable vertical scrolling within data-container while maintaining text wrapping functionality.

Jade Code Snippet

extends ./layout.jade

block content
    #maindiv
        .sidebar
            // Sidebar Content
        header
            // Header Content
        .container
            // Container Content

Associated CSS Styles

.container {
  // CSS styles for container
}

// Additional CSS styling properties

JavaScript Animation Functionality

// JavaScript animation functions

// Sample JavaScript code block provided
// Sample CSS code block provided
<div id="maindiv">
  <div class="sidebar">
    // Sidebar HTML content
  </div>
  <header>
    // Header HTML content
  </header>
  <div class="container">
    // Main container HTML content
  </div>
</div>

Answer №1

If you want to achieve this in a clean and straightforward manner, consider implementing it with flexbox. Take a look at my approach:

#container {
    display: flex;
}

.item1, .item2 {
    display: flex;
    min-width: 100%;
    white-space: normal;
}

This method also provides vertical scrolling when the contents of any div exceed the height.

Answer №2

Feeling impatient, I decided to take matters into my own hands and find a solution. While not exactly what I had envisioned or flawless, it does give the impression of meeting my goals.

To achieve this, I modified the CSS properties for elements like .today, setting it to display: none, adjusting #data-container with overflow-y: auto and overflow-x: hidden, and making changes to the animation effects.

The updated animation now shifts the current division to a different location before bringing it back through an animated process.

Updated Animation Script

$('.navbar-inside-two').click(function() {
        if($(this).hasClass('above')) {
            $(".today").css({
                display: 'inline-block',
                left: $("#data-container").width()
            });
            $(".project").css('display', 'none');
            $(".project, .today").animate({left: '0px'}, 150);
            $(".navbar-inside-one").removeClass('below').addClass('above');
            $(this).removeClass('above').addClass('below');
        }
    });

    $('.navbar-inside-one').click(function() {
        if($(this).hasClass('above')) {
            $(".project").css({
                display: 'inline-block',
                left: '-' + $("#data-container").width()
            });
            $(".today").css('display', 'none');
            $(".project").animate({left: "0"}, 150);
            $(".navbar-inside-two").removeClass('below').addClass('above');
            $(this).removeClass('above').addClass('below');
        }
    });

Answer №3

Here is a helpful suggestion:

#info-block {
    white-space: nowrap;
}
.section, .daily-info {
    white-space: normal;
}

You may also want to try using #info-block {overflow-y: auto} to eliminate the unsightly scrollbar without sacrificing functionality.

An additional option is to set the width of #info-block to 200%, and give its children a width of 50%, then slide #info-block.

Answer №4

word-wrap: break-word doesn't function properly with white-space: nowrap, so it is necessary to specifically target .project children paragraph.

Simply remove the following CSS from .project.

word-wrap: break-all;

Then add the following CSS to your code

.project p {
   white-space: normal;
}

I have made the above changes in the code snippet below, hoping it will assist you. Thank you

$(document).ready(function() {

  //Height function for container and sidebar
  (function() {
    $(".container, .sidebar").height($("#maindiv").height() - $('header').height());
    $(".sidebar").css('top', 49); //TO BE MADE AGAIN
  })();

  $('span').click(function() {
    var sidebar = $('.sidebar').css('left').replace(/([a-z])\w+/g, '');
    if (sidebar < 0) {
      $('.sidebar').animate({
        'left': '0px'
      }, 200);
      $('.container').animate({
        'left': '150px'
      }, 200)
    } else {
      $('.sidebar').animate({
        'left': '-150px'
      }, 200);
      $('.container').animate({
        'left': '0px'
      }, 200)
    }
  });

  $('.navbar-inside-two').click(function() {
    $(".project, .today").animate({
      left: "-" + $("#data-container").width()
    }, 200);
    $(".navbar-inside-one").removeClass('below').addClass('above');
    $(this).removeClass('above').addClass('below');
  });

  $('.navbar-inside-one').click(function() {
    $(".project, .today").animate({
      left: "0"
    }, 200);
    $(".navbar-inside-two").removeClass('below').addClass('above');
    $(this).removeClass('above').addClass('below');
  });
});
/* Messed up Css from multiple Sass files */

.font-head,
.navbar,
.sidebar {
  font-family: 'Poiret One', cursive;
  font-weight: 100;
  letter-spacing: 2.2px;
}
.font-para,
input[type='text'] {
  font-family: 'Source Sans Pro', sans-serif;
  font-weight: 100;
  letter-spacing: 1.4px;
}
* {
  box-sizing: border-box;
  -webkit-font-smoothing: antialiased;
  font-family: 'Source Sans Pro', sans-serif;
}
html,
body,
... [TRUNCATED FOR BREVITY] ...e Google API -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

What method does jQuery Validation use to configure the validation message?

A custom method was developed for the jQuery validation plugin to validate whether a given value meets the length requirements set during validation. The method is structured as follows: jQuery.validator.addMethod("exactLength", function(value, ...

Uniquely tag an uploaded file

My code for uploading files is as follows: var xhr = new XMLHttpRequest(); xhr.upload.addEventListener("progress", uploadProgress, false); xhr.open("POST", requestUrl, true); xhr.send(f); I want to draw your attention to the fact that I have attached a l ...

Issues with managing ajax response handlers

Just dipping my toes into the world of ajax and attempting to create a reusable ajax request function. According to Firebug, the request is successfully fetching the correct data from my php file. However, for some reason, the response isn't getting i ...

Can you explain the distinction between $scope.$root and $rootScope?

When looking at controllers, I noticed that $scope includes $root. Can you explain what $root is and how it differs from the $rootScope that can be injected into the controller? ...

Using Redux with Next.js to implement getStaticPaths

Can someone help me understand how to implement getStaticPaths in conjunction with Redux in Next.js? I'm currently using next-redux-wrapper to manage my content, but I am encountering issues when trying to display the data. Below is a snippet of my ...

The compatibility between Javascript and AJAX functions is currently not functioning as expected

I am attempting to send some data to the server using AJAX with the value obtained from a JavaScript variable. Here is the code snippet: <script type="text/javascript> var url; function applyPhoto(_src) { url = _src; var pho ...

What are the steps to implement $navigateTo() in a NativeScript Vue application?

What could be causing $navigateTo(Page) to not work? Visit this link for more information ...

Creating a conditional query in Mongoose: A step-by-step guide

The code below functions without any query strings or with just one query string. For example, simply navigating to /characters will display all characters. However, if you specify a query string parameter like /characters?gender=male, it will only show ma ...

Activate video playback when scrolling, but ensure it only occurs one time

I've encountered an issue with my script that is meant to play a video when it reaches a certain position on scroll. The problem is, if the video is paused and scrolling continues, it starts playing again. I attempted to use just one scroll function b ...

Is it possible to convert a blob to an image file using the FileReader in HTML

client side code <head> <script> var reader = new FileReader(); var objVal; var image = new Image(); reader.onload = function(e) { document.getElementById('propertyImg').setAttribute('src', e.target.result); }; fun ...

Create an array using modern ES6 imports syntax

I am currently in the process of transitioning Node javascript code to typescript, necessitating a shift from using require() to import. Below is the initial javascript: const stuff = [ require("./elsewhere/part1"), require("./elsew ...

Creating JSON on iPhone using jQuery is a straightforward process that involves using the built-in

Is there a way to create a JSON object on an iPhone browser without using the "JSON" object? var json_string = JSON.stringify(array); I have been able to make a JSON object on a web browser using this code, but I'm not sure how to do it on an iPhone ...

Can CSS be altered dynamically in Laravel blade?

Is there a way to dynamically change CSS? I am trying to set the class=sheet padding-top: 28mm; when the size of $anArray is less than 30. If the array has more than 30 elements then apply padding-top: 28 * 2 mm;. Finally, if the array exceeds 60, use pad ...

Avoid reloading the page when the form is submitted using form.trigger('submit') command

My current code functions properly when the user clicks on the form's submit button: // load dashboards when filter form is submitted $('div.active form.filter-form').submit(function (e) { // get subm ...

Is there a way to make a Bootstrap grid with four columns span across two rows in the second column?

How can I create a Bootstrap grid with four columns where the second column spans two rows? Hello, I am looking for help to achieve a portfolio grid layout with 4 columns (refer image here) and have the second column span 2 rows. I plan to include images ...

Creating multiple instances of an object

When using Javascript, I am trying to create an object in the following way: var testObject = { value: "this is my initial value", setup: function() { value: "foo" } }; Now, my goal is to instantiate this object and have different val ...

Ways to extract the id after clicking

In my code, I have a query called snapshot.forEach that functions similarly to a for loop in looping through all of my Firebase data and displaying it with a div tag containing a click event. When another user clicks on this div tag, the event will retriev ...

Tips for editing bootstrap-vue table columns using non-Latin characters?

I need to create a table using the Cyrillic alphabet, but the keys of the object must be in the Latin alphabet within the program. Example : export default { data() { return { wmsFields: ['№', 'Наименование', ...

What is the best way to sort my API responses to display only users who are either currently online or offline?

Hi everyone, I've made great progress on my project so far without any assistance (pretty proud of myself), but now I could use some help. I'm working on creating a tabbed menu that filters the results of my API calls to display: All users, Onlin ...

The useNavigate() hook from react-router-dom is not properly setting the id in the URL path

I am currently using react-router-dom v6 for my routing needs. My goal is to pass an ID in the navigate URL path. Here is the onClick method and button code that I am working with: let navigate = useNavigate(); const routeChange = (id) => { let ...