1

Tried writing "transition: height 1s ease-out;" in Internal CSS, inline CSS, javascript but nothing works. I am using Chrome(latest as of now). It works when I had different funtions like onmouseover to open the shutter and onmouseclick to close it.

<script>

    function departmentShutter(){
        if(document.getElementById("departmentShutter").clientHeight === 100 ){
            document.getElementById("departmentShutter").style.height = "inherit";
        }
        else{
            document.getElementById("departmentShutter").style.height = "100px";
        }
    }

    function studentShutter(){
        if(document.getElementById("studentShutter").clientHeight === 100 ){
            document.getElementById("studentShutter").style.height = "inherit";
        }
        else{
            document.getElementById("studentShutter").style.height = "100px";
        }
    }

</script>

The CSS is as follows: Just focus on transition to work.

.dashboard{
    width: 100%;
    height: fit-content;
    position: fixed;
}
.dashboardContent{
    height: -webkit-fill-available;
    width: 100%;
    margin: 0px 0px 0px 0px;
    padding: 0px 0px 0px 0px;
    overflow-x: auto;
}
.department{
    height: 100%;
    width: 50%;
    left: 0px;
    display: inline-block;
    float: left;
    z-index: 0;
    position: fixed;
    margin-top: 100px;
}
.student{
    height: 100%;
    width: 50%;
    right: 0px;
    display: inline-block;
    float: left;
    z-index: 0;
    position: fixed;
    margin-top: 100px;
}
.departmentShutter{
    height: inherit;
    transition: height 1s ease-out;
    width: 50%;
    left: 0px;
    display: inline-block;
    background-color: #09d;
    float: left;
    z-index: 99;
    position: fixed;
}
.studentShutter{
    height: inherit;
    transition: height 1s ease-out;
    width: 50%;
    right: 0px;
    display: inline-block;
    background-color: #2d0;
    float: left;
    z-index: 99;
    position: fixed;
}
.departmentShutter span,.studentShutter span{
    font-size: 5em;
}
.rectangle{
    height: 200px;
    width: 200px;
    background-color: #78015d;
}    

HTML:

<div class="dashboard">
    <div class="dashboardContent">

        <div id="departmentShutter" class="departmentShutter cursorPointer disable-selection" onclick="departmentShutter()">
            <span class="center">Department</span>
        </div>
        <div class="department">
            <table>
                <tr>    
                    <td><div class="rectangle"></div></td>
                </tr>
            </table>
        </div>

        <div id="studentShutter" class="studentShutter cursorPointer disable-selection" onclick="studentShutter()">
           <span class="center">Student</span>
        </div>
        <div class="student">
            <table>
                <tr>    
                    <td><div class="rectangle"></div></td>
                </tr>
            </table>
        </div>

    </div>  
</div>

1 Answers1

3

Transitions only work on values that can be converted to numbers and values that have been explicitly set on an element up front, so that the CSS rendering engine can determine the progression from the start value to the end value. inherit is not a value that is numeric, so the transition doesn't work.

Change height:inherit to height:100% in the .departmentShutter and .studentShutter classes as well as in the JavaScript.

Also, there is no need for two separate functions for the sizing of the two separate div elements since the two functions do exactly the same thing, just on different elements. The two functions can be combined into one and to determine which element needs to be sized, you need only use the this keyword, which will be bound to whichever div initiated the event in the first place.

Lastly, don't use inline HTML event attributes (onclick, etc.) to bind your event handlers. That is how it was done 20 years ago, but unfortunately it just keeps getting copied and pasted today, so new users don't know any better. There are many reasons not to use this technique anymore. Instead, separate your JavaScript completely from your HTML and follow modern standards for event binding.

// Get your DOM refernces just once to avoid excessive DOM scans
// Find both div elements that should be clickable and place them both in an array
var shutters = Array.from(document.querySelectorAll(".departmentShutter, .studentShutter"));

// Loop through the array...
shutters.forEach(function(shutter){
  // Assign a click event handler to each
  shutter.addEventListener("click", function(evt){
  
    // Loop through array and reset heights of both shutters
    shutters.forEach(function(shutter){ 
      shutter.style.height= "100%";
    });
    
    if (this.clientHeight === 100) {
      this.style.height = "100%";
    }
    else {
      this.style.height = "100px";
    }
  });
});
.dashboard {
  width: 100%;
  height: fit-content;
  position: fixed;
}

.dashboardContent {
  height: -webkit-fill-available;
  width: 100%;
  margin: 0px 0px 0px 0px;
  padding: 0px 0px 0px 0px;
  overflow-x: auto;
}

.department {
  height: 100%;
  width: 50%;
  left: 0px;
  display: inline-block;
  float: left;
  z-index: 0;
  position: fixed;
  margin-top: 100px;
}

.student {
  height: 100%;
  width: 50%;
  right: 0px;
  display: inline-block;
  float: left;
  z-index: 0;
  position: fixed;
  margin-top: 100px;
}

.departmentShutter {
  height: 100%;
  transition: height 1s ease-out;
  width: 50%;
  left: 0px;
  display: inline-block;
  background-color: #09d;
  float: left;
  z-index: 99;
  position: fixed;
}

.studentShutter {
  height: 100%;
  transition: height 1s ease-out;
  width: 50%;
  right: 0px;
  display: inline-block;
  background-color: #2d0;
  float: left;
  z-index: 99;
  position: fixed;
}

.departmentShutter span, .studentShutter span {
  font-size: 5em;
}

.rectangle {
  height: 200px;
  width: 200px;
  background-color: #78015d;
}
<div class="dashboard">
  <div class="dashboardContent">

    <div id="departmentShutter" class="departmentShutter cursorPointer disable-selection percentHeight">
      <span class="center">Department</span>
    </div>
    <div class="department">
      <table>
        <tr>
          <td><div class="rectangle"></div></td>
        </tr>
      </table>
    </div>

    <div id="studentShutter" class="studentShutter cursorPointer disable-selection percentHeight">
      <span class="center">Student</span>
    </div>
    <div class="student">
      <table>
        <tr>
          <td><div class="rectangle"></div></td>
        </tr>
      </table>
    </div>

  </div>
</div>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • i've never used `inherit` explicitely and truth be told, i'm to lazy to try. Wouldn't a transition work on an element with `height: inherit` if its parent has a height set? – malifa Nov 10 '17 at 18:44
  • @lexith No (as you have already seen). `inherit` causes the element's property value to be computed, rather than have a definite value up front. Transitions can't/don't work on computed values that are inherited, they must have a definitive value set to start from and one to end at. – Scott Marcus Nov 10 '17 at 18:53
  • @ScottMarcus I am stuck again. I want that if I click on studentShutter then departmentShutter should be closed and vice-versa using JS. What changes do you recommend in JS? – Arpan Singh Kushwaha Nov 11 '17 at 06:53
  • @ArpanKush I have updated the answer to do that for you. It's just a matter of resetting both sides to their original heights before sizing just the one that was clicked. Please mark this as "the" answer. – Scott Marcus Nov 11 '17 at 16:54