Mobile devices have a fixed distance and center alignment on the horizontal timeline

I am attempting to create a horizontal timeline similar to this example: https://codepen.io/ritz078/pen/LGRWjE

The issue I am facing is that I do not have specific dates (DD/MM/YYYY) but only ranges such as 1998-2002 or single years like 2009. This has caused a problem that I am struggling to resolve, resulting in the display issue shown here:

https://i.sstatic.net/4C7xN.png

My objectives are as follows:

  1. Establish a fixed distance between timeline elements
  2. Create functionality that works solely with years
  3. For devices less than 768px wide, ensure only one element is displayed and centered

I have managed to find a solution for objective 3., however, I am facing challenges with the other requirements:

if ($(window).width() < 768 {
    eventsMinDistance = $('.cd-horizontal-timeline .events-wrapper').width(/2;)
}

(timelines.length > 0) && initTimeline(timelines);

$(window).resize(function(){
    if ($(window).width() < 768 {
    eventsMinDistance = $('.cd-horizontal-timeline .events-wrapper').width(/2;)
    } else{
        eventsMinDistance = 155;
    }
}

If anyone knows how to address these issues, I would greatly appreciate your assistance. I have been struggling for hours without success. Thank you for your help!

Answer №1

Your code initially had a syntax issue, but I fixed it and made some additional adjustments to achieve the desired fixed width between each date. Debugging and understanding the developer's intention made this process quite straightforward. See below for the updated code:

jQuery(document).ready(function($){
    var timelines = $('.cd-horizontal-timeline'),
        eventsRelativeDistance = false;
    if ($(window).width() < 768) {
        eventsMinDistance = Number($('.cd-horizontal-timeline .events-wrapper').width())/2;
    } else {
        var eventsMinDistance = 100;
    }
    
    (timelines.length > 0) && initTimeline(timelines);
    
    $(window).resize( function(){
        if ($(window).width() < 768) {
            eventsMinDistance = Number($('.cd-horizontal-timeline .events-wrapper').width())/2;
        } else {
            eventsMinDistance = 100;
        }
    });
    
    // Rest of the code continues...
});

Answer №2

Have you had any success resolving this issue? I am facing a similar problem where adjusting the default distance between dates is causing the timeline layout to break. The article explains the calculations for determining the distances between dates in detail.

The main.js file defines a minimum distance between consecutive dates using the eventsMinDistance variable, which is initially set to 60 pixels. The differences between each date are calculated based on the data-date attribute assigned to them. The minimum difference serves as a reference point for setting the distances between dates along the timeline.

For instance, if the smallest difference found is 5 days, then the distance between two dates separated by a 5-day interval will be 60 pixels, while the distance between dates separated by 10 days will be 120 pixels.

source:: www.codyhouse.org

The code snippet below specifies the minimum distance:

var timelines = $('.cd-horizontal-timeline'),
    eventsMinDistance = 60;

(timelines.length > 0) && initTimeline(timelines);

Changing the min distance value from 60 to 1 disrupts the application because the distances become too small. The function setDatePosition calculates the positioning of dates based on the specified minimum distance and will break if modified.

function setDatePosition(timelineComponents, min) {
    for (i = 0; i < timelineComponents['timelineDates'].length; i++) {
        var distance = daydiff(timelineComponents['timelineDates'][0], timelineComponents['timelineDates'][i]),
            distanceNorm = Math.round(distance/timelineComponents['eventsMinLapse']) + 2;
        timelineComponents['timelineEvents'].eq(i).css('left', distanceNorm*min+'px');
    }
}

It seems that the tutorial serves as a good reference but customization may require rewriting the code from scratch to fit specific needs. It is crucial to understand the logic behind the calculations before making adjustments to avoid breaking the functionality of the timeline.

If you were able to find a solution, please share your insights. I have been experimenting with date manipulation to achieve equal spacing but encounter issues when adding multiple dates. This has led me to consider developing a custom solution tailored to my requirements.

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

Utilize the power of REACT JS to transform a specific segment within a paragraph into a hyperlink. Take advantage of the click event on that hyperlink to execute an API request prior to

In React JSX, I'm encountering an issue trying to dynamically convert a section of text into an anchor tag. Additionally, upon clicking the anchor tag, I need to make an API call before redirecting it to the requested page. Despite my attempts, I have ...

Launching URLs from JSON data in the system browser - Apache Cordova

I am facing an issue with opening links from a JSON in the system browser on both Android and iOS devices. The link generated by the function appears as: {{item.addressLink}}, instead of bit.ly/xyzxyz Here is what I currently have: <a href="#" ng-cl ...

Styling CSS Based on Input from Child Siblings

Is it possible to customize an adjacent element based on a valid input from a child element? Let's say we have the following HTML structure: <div id="source"> <input type="text"> </div> <div id="target"> </div> Below ...

Divide the width by half in a CSS grid layout with 3 columns

Is it possible to replicate the layout shown in the image using CSS grid? https://i.sstatic.net/XSoE8.png ...

utilize images stored locally instead of fetching them from a URL path in a Vue.js project

Hey there fellow Developers who are working on Vuejs! I'm encountering something strange in the app I'm building. I am attempting to modify the path of image requests based on a particular result, which causes the images to change according to th ...

How can we ensure that href/src values are "absolutized" when utilizing server rendering?

Imagine having an element with its href/src set as a root path like this: href="/abc/def". There is also a function written like this: function absolutizeHREF(href) { const URLBase = new URL(`http://${process.env.HOST}:${process.env.PORT}/`); ...

Leveraging various techniques within a single controller in AngularJS

I am seeking assistance and advice on a coding issue I am facing. I am attempting to use two methods in one controller. The first method is to display selected rooms, while the second method is to display selected pax. However, only the first method seems ...

What could be the reason for the loss of default browser focus on this accordion navigation?

Why is the browser default focus being lost on this accordion navigation? <div class="panel panel-default"> <div class="panel-heading"> <button aria-controls="collapseTwo" aria-selected="false" data-target="#collapseTwo" data-to ...

What exactly occurs when a "variable is declared but its value is never read" situation arises?

I encountered the same warning multiple times while implementing this particular pattern. function test() { let value: number = 0 // The warning occurs at this line: value is declared but its value is never read value = 2 return false } My curi ...

Unable to navigate to partial view within MEAN application

I'm currently following a tutorial on creating single page applications using the MEAN stack. So far, I have successfully rendered the index.jade view. However, I encountered an issue when trying to route to a partial view as the DOM of the page does ...

Fade effects on hover with a changing background color

Here is the CSS code to be used: #onec-fourth:hover { background-color: #F36DE1; color: white; } I am looking to keep the background color and text color effects for 1 second after I move my mouse off the object (#onec-fourth). Currently, when I mov ...

Creating a jQuery alert similar to Stack Overflow's and integrating it with server-side functionality

I recently asked a question but unfortunately couldn't find the answer I was looking for. I came across this discussion on Stack Overflow about creating an alert box with dynamic data, but it wasn't exactly what I needed. After searching online ...

Unable to upload files using Jquery.form (3.51.0-2014.06.20) in IE9 within a Sharepoint application

Before diving into this issue, I want to clarify that I have thoroughly researched similar problems on Stack Overflow and other forums related to jQuery form upload problems. However, I am confident that the issue I am experiencing is unique. The problem ...

Tips for modifying the color of selected text

Is there a way to change the color of the dropdown text? Currently, it is set to red but initially appears as black. When clicked on, it changes to red. How can I make it so that the color is initially displayed as red? <select> <option style ...

Import a new JavaScript file and access a variable

I'm currently working on a school project where I am creating an online platform for purchasing games. The platform is designed using HTML, CSS, and JS, with each game having its own corresponding JS file containing the information. Here is a sample o ...

Is there a way to continuously send out this ajax request?

My attempt to use setInterval for sending an AJAX request every 2 seconds is causing the page to crash. It seems like there is something wrong in my code! Check out the code below: var fburl = "http://graph.facebook.com/http://xzenweb.co.uk?callback=?" ...

TypeScript Redux actions not returning expected type

Basically, I am attempting to assign types to a group of functions and then match them in my Redux reducer. Here are the functions I have defined in actions.ts: export const SET_CART_ITEMS = "SET_CART_ITEMS"; export const SET_CART_TOTALS = "SET_CART_TOTA ...

Issue: Compilation unsuccessful due to an error in the Babel loader module (./node_modules/babel-loader/lib/index.js). Syntax error

I'm currently exploring how to integrate the Google Maps Javascript API into my VueJs project without relying on the vue2-google-maps package (as it seems too restrictive to me). Here's what I've done so far, after registering my Vue compon ...

Sorting Angular components in reverse alphabetical order from A to Z

I feel like the answer to this problem is staring me right in the face, but no matter how hard I look, I just can't seem to find it. When dealing with a large ng-repeat, I have multiple sort options that I pass as an array. The user has the ability t ...

Different ways to determine if a given string exists within an Object

I have an object called menu which is of the type IMenu. let menu: IMenu[] = [ {restaurant : "KFC", dish:[{name: "burger", price: "1$"}, {name: "french fries", price: "2$"}, {name: "hot dog", d ...