Stylish date picker in CSS

Here is a design mockup along with my current solution. The issue arises when two numbers are adjacent to each other (either horizontally or vertically) as they should merge, illustrated in the mockup below.

For example, when selecting numbers 48-49-50, they should appear like this:

https://i.sstatic.net/9NILe.png

Similarly, numbers 38-39-40-41 from the next screenshot should look like this:

https://i.sstatic.net/HajLk.png

To achieve this effect, I utilize AddClass("active") with the following properties for each clicked number:

  a.active {
    background-color: #3E9EA5;
    border-radius: 9px 9px 9px 9px;
    color: white;
  }

Answer №1

To achieve the merge effect with CSS selectors and javascript, you can utilize jQuery for simplified coding.

The solution involves examining the clicked element along with its surrounding siblings (both above and below) to determine the merging behavior. Comments have been included within the code for clarity. Feel free to reach out if you have any questions or require further assistance.

$(document).ready(function(){
  var awesomeDates = $(".awesome-date");
  
  awesomeDates.on("click", function(){
    //Variables to store neighboring elements
    var prevElement, nextElement, topElement, bottomElement;
    //Toggle class functionality
    if($(this).hasClass("active")){
      $(this).removeClass("active");
      prevElement = $(this).prev();
      if(prevElement.hasClass("merge-right")){
        prevElement.removeClass("merge-right");
      }
      topElement = $(this).prevAll().eq(9);
      if(topElement.hasClass("active")){
        $(this).removeClass("merge-top");
        topElement.removeClass("merge-bottom");
      }
      bottomElement = $(this).nextAll().eq(9);
      if(bottomElement.hasClass("active")){
        $(this).removeClass("merge-bottom");
        bottomElement.removeClass("merge-top");
      }
    }else{
      $(this).addClass("active");
      prevElement = $(this).prev();
      nextElement = $(this).next();
      if(prevElement.hasClass("active")){
        prevElement.addClass("merge-right");
      }
      if(nextElement.hasClass("active")){
        $(this).addClass("merge-right");
      }
      topElement = $(this).prevAll().eq(9);
      if(topElement.hasClass("active")){
        $(this).addClass("merge-top");
        topElement.addClass("merge-bottom");
      }
      bottomElement = $(this).nextAll().eq(9);
      if(bottomElement.hasClass("active")){
        $(this).addClass("merge-bottom");
        bottomElement.addClass("merge-top");
      }
    }
  });
});
.dater {
  display: block;
  width: 330px;
  height: 250px;
  background-color: #f7f7f7;
}

/* CSS styles for dater list */
.dater-list {
  margin: 0;
  padding: 2px;
  height: auto;
}

.dater-list .awesome-date {
  display: inline-block;
  float: left;
  width: 20px;
  height: 20px;
  padding: 5px;
  border-radius: 50%;
  text-align: center;
  background-color: #fff;
  margin: 1px 1px 0 1px;
  transition: all 1s ease;
}

.dater-list .awesome-date.active{
  background-color: #1abc9c;
  color: #fff;
}
/* Styling for merged date elements */
.dater-list li.awesome-date.active + li.awesome-date.active:not(:nth-child(10n+1)){
  border-bottom-left-radius: 0;
  border-top-left-radius: 0;
  margin-left: 0;
  padding-left: 6px;
}

.dater-list li.awesome-date.active.merge-right:not(:nth-child(10n)){
  border-bottom-right-radius: 0;
  border-top-right-radius: 0;
  margin-right: 0;
  padding-right: 6px;
}

.dater-list li.awesome-date.active.merge-top:not(:nth-child(-n+10)){
  border-top-right-radius: 0;
  border-top-left-radius: 0;
  margin-top: 0;
  padding-top: 6px;
}

.dater-list li.awesome-date.active.merge-bottom{
  border-bottom-right-radius: 0;
  border-bottom-left-radius: 0;
  margin-bottom: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="dater">
  <ul class="dater-list">
  <!-- Placeholder HTML content to be dynamically populated -->
  </ul>
</div>

If you need assistance with integrating this code into your project or have any queries, don't hesitate to ask. Also, remember to include the initial HTML and CSS next time for a smoother development process.

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

Unable to access JSON data from LocalStorage using jQuery

Currently, I am encountering an issue while attempting to save a JSON array in jQuery to localStorage for future use. Unexpectedly, whenever I make the attempt to save it, an error indicating that the object is not defined arises. Below is my present code: ...

Using BootstrapValidator for conditional validation

While utilizing the BootstrapValidator plugin for form validation, I encountered a specific issue. In my form, I have a "Phone" field and a "Mobile" field. If the user leaves both fields blank, I want to display a custom message prompting them to enter at ...

Encountered a problem with sending values using jQuery and JSON: Unexpected token 'b'

Can you explain why in this specific scenario there is an error showing up as SyntaxError: Unexpected token b listados:229? response = '{"name":"John"}'; var obj = jQuery.parseJSON(response); $.ajax({ url:urlform, dataType: ...

The concept of CSS background-position

Hey there, I have a query regarding a sprite image that contains icons with both normal and hover effects. Here is the current CSS code I am using: .wi{ background-image:url(images/icons/small/wi.png); background-repeat:no-repeat; display:blo ...

hide content on both desktop and mobile devices

I can't seem to figure out what's wrong here and my mind is blank. I have 2 tables - one for desktop users and one for mobile users. I attempted to hide one using the display none property in my code. .mobile{display: none;} This CSS was mean ...

Create a stylish layout with two div elements positioned side by side using flexbox for a responsive design

Struggling with my portfolio website, I encountered a challenge. I attempted to place two divs side by side, each containing text and an image. While the initial setup was simple, trying to make it responsive using flexbox has proven to be quite frustratin ...

Understanding the scope of an anonymous function after a successful jQuery ajax call

Is it possible to modify the returnHtml variable from within the success function without using its name as a parameter? function retrievePrice(productId, storeId) { var returnHtml = ''; jQuery.ajax({ url: "/includes/data.jsp?" ...

Is there a way to prevent my table from surpassing the width of my card using CSS?

My table inside a card is not responsive on small screens, causing it to go outside the card. I've tried various solutions like using flex on the parent element and setting the table width equal to the card but nothing seems to work. The card should a ...

Animated Gradient Header with CSS

I've been attempting to add an animated gradient to my header class, but so far I haven't been successful. I want the gradient to apply only to the header, not the body. Here's the link I'm using for reference: Can anyone offer some i ...

Passing PHP values to JavaScript and then to AJAX requires using the appropriate syntax and techniques for

Currently, I am populating a table with data retrieved from my database. Here is a snippet of the code: <?php //mysqli_num_rows function while($row=mysqli_fetch_array //I know this may be wrong, but that's not the point echo "<tr><t ...

What could be causing the error message "(#tabs).tabs is not a function" in the context of setting up JQuery in Rails 3.1.3?

When attempting to load the login page on localhost:3000, an error in the application.js file appears in firebug causing the page to hang. The Gemfile contains: gem 'jquery-rails', '~>1.0.19' The error-causing code (including the r ...

Exploring the world of JSON and JSONP with JQuery

I'm experiencing some issues with my $.getJSON method, whereas the $.ajax method works perfectly fine. Let's first take a look at my getJSON call: $.getJSON("http://localhost:1505/getServiceImageList?callback=loadImagesInSelect", loadImagesInSel ...

Having trouble getting Ajax to function properly with CodeIgniter

Here is the AJAX code snippet: $.ajax({ url: '<?php echo base_url(); ?>deleteRowUsingApiKey/index', //This is the current doc type: "POST", //dataType:'json', // add json datatype to get json data: {name ...

Interactive Google Maps using Autocomplete Search Bar

How can I create a dynamic Google map based on Autocomplete Input? Here is the code that I have written: <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDeAtURNzEX26_mLTUlFXYEWW11ZdlYECM&libraries=places&language=en"></scri ...

What is the best way to implement style guidelines that are associated with a model in AngularJS?

In the process of creating an application where 1-5 views along with the corresponding model and controller are sent to the client upon page load. The user navigates through each view in sequence using routing, which is functioning correctly. However, I a ...

Placing two items at opposite extremes of a full-width display with a header

Despite the abundance of resources on CSS positioning, I constantly find myself revisiting this topic because it continues to perplex me with no clear solution in sight. Let's simplify things. Imagine a header div with a width of 100%, like this: &l ...

get the data from a database table by specifying the ID and retrieve the corresponding column value

I am struggling to retrieve the value of a database column based on its ID. My goal is to show it in a <span> with the ID "#name_page" I pass the ID via POST using "$(this).attr("id")" This is my AJAX request $(document).on('click', &apo ...

Adjusting Flexslider to perfectly accommodate the height and width of the window dimensions

Currently, I am using Flexslider version 1.8 and seeking to set up a fullscreen image slider on my homepage. My goal is to adjust the slider to match the browser window size. While experimenting with width:100% and height:auto properties, I have managed t ...

What causes Font Awesome 5 icons to vanish when changing the font-family?

I am facing an issue with the code I have below. It changes the font style successfully, but it also causes the icon to disappear. Can anyone provide tips on how to ensure that the icon remains visible while changing the font style? <link rel="styles ...

Stop the animation when the mouse is moved

Below is the code I am working with: $(source) .on('mouseenter', start) .on('mouseleave', stop) .on('mousemove', zoom.move); Within this code, I have attached several mouse event listeners. When the 'mouseenter' ev ...