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

Customize the element of the root node of a MUI component using the styled()

I am trying to implement the "component" prop with a MUI component (such as ListItem) using the styled() API. However, I am facing an issue where it says that "component" is not a valid prop. Can someone guide me on how to correctly achieve this? I have se ...

Having trouble retrieving the JSON response attribute

Upon receiving a JSON response in JavaScript, the structure is as follows: {"opt1":"2.92","opt2":"4.24","opt3":"6.36"}; This is what happens when console.log(data) is implemented with data being the response: success: function(data){ console ...

What is the best method for extracting Google Custom Search results through parsing JSON with a RESTful API to retrieve results exactly as Google displays them?

I am looking for a solution to properly parse the JSON data retrieved from the Google Custom Search RESTful API into results that resemble what Google displays in its search results. Currently, I am utilizing AJAX for the service call, but I am unsure of ...

Attempting to execute a function within a MAP operation

My attempt to integrate a function into my map is resulting in only the function running and not the rest of the code. Edit: After reflecting on the situation, it dawned on me that I need to elaborate further. The goal here is to execute this.math in orde ...

How to toggle classes on specific items generated with Vue JS's v-for directive

I have a list rendering using the v-for directive in Vue.js. <li v-for="group in groupList" :key="group.id" @dragenter="toggleClass ...."@dragleave="toggleClass ...." > Content </li> My goal is to apply a class to the li el ...

What impact does adding 'ng' in unit tests have on how promises are handled?

Here is an example of a service that utilizes $q.when to wrap a promise from a third-party library: // myService.js angular.module('myApp', []) .service('myService', function($q, $window) { var api = new $window.MyAPI(); this ...

Error: Unable to locate module: Unable to resolve '@/src/components/layout/Header'

Compilation Error Encountered issue in Next.js (version 14.2.4) while trying to locate '@/src/components/layout/Header' at ./src/app/layout.js:3:1 Suddenly, 2 days ago the code was functioning properly but now it's throwing this error. Er ...

Error message: Angular JS encountered an issue when trying to initiate the test module. The cause

When I run JavaScript within an HTML file and use AngularJS with ng-repeat function, I encounter this console error: angular.js:38 Uncaught Error: [$injector:modulerr] Clicking on the link in the error message leads to: Failed to instantiate module test ...

Create compelling visual representations by utilizing Google charts to draw customized charts based on variable

I have been attempting to create a chart using Google Charts by passing some parameters to the function. I am trying to use these parameters to populate the data, but it doesn't seem to be working as expected. Is it possible to include parameters in t ...

Tips on hovering over information retrieved from JSON data

Within my code, I am extracting information from a JSON file and placing it inside a div: document.getElementById('display_area').innerHTML += "<p>" + jsonData[obj]["name"] + "</p>"; I would like the abi ...

ClassNames Central - showcasing a variety of class names for interpolation variables

Seeking guidance on creating a conditional statement to set the className for a div element. The conditional is functioning correctly, and I can see the className being assigned properly in the developer console. However, I am struggling to return it as ...

Do you find this unattractive? What are some ways to improve this unsightly JavaScript statement?

This code seems messy, how can I better structure this switch statement? function renderDataTypeIcon(dataType: string) { let iconName; switch (dataType) { case "STRING": //TODO - ENUM iconName = "text"; break; ...

Tips for positioning the search form in the navbar header

My brand image and list with 2 glyph-icons & a search form in the navbar header are not aligning properly. The search form button keeps dropping to the line below. Does anyone know a CSS trick to keep it on the same line as the rest of the links? All the ...

How to create a transparent background image in a Jekyll theme without impacting the visibility of the foreground

I'm new to HTML and CSS, currently working with a Jekyll boostrap landing theme from GitHub to create a static responsive website. I'm looking to change the background image on the theme and make it transparent to enhance the visibility of the fo ...

Ways to troubleshoot JavaScript following an AJAX request?

My webpage is structured into three separate files. The index.html file contains a navigation bar, a content box, and a footer within 3 divs. Additionally, there are two other .html files with different content that should load upon clicking specific links ...

Tips for eliminating the backslash introduced by JQuery

Switching back from framework 4.5 to 4.0 caused several issues that needed fixing. One significant change I noticed was that jQuery started escaping double quotes. Is there a way to stop this behavior? I attempted datatest = datatest.replace("\\ ...

Issue with Heroku deployment: unable to locate JavaScript file

I encountered an issue while deploying my node.js app. Everything seemed to be working fine but it couldn't locate the JavaScript files. The error message displayed was: proove.herokuapp.com/:16 GET 404 (Not Found) Here is the server.js code snip ...

My JavaScript array is not working with the stringify function

I am trying to encode my JavaScript array into JSON using stringify. Here is the code: params["margin_left"] = "fd"; params["text"] = "df"; params["margin_to_delete"] = "df"; console.info(params); When I check the output in Chrome console, it shows: [m ...

Tips for styling Ajax.ActionLink elements with CSS

Hi there, I'm trying to style a navigation bar using CSS. I'm pulling the navigation bar values from a database using Ajax.ActionLink. I attempted to use JavaScript but encountered an error stating "jQuery is not defined", Here's the cod ...

Setting up a div as a canvas in Three.js: Step-by-step guide

Is there a way to adjust the JavaScript in this three.js canvas example so that the scene can be contained within a specific div element on a webpage? Here is the example: https://codepen.io/PedalsUp/pen/qBqvvzR I would like to use this as the background ...