size watcher v1

Triggering the scroll event by decreasing content size

by smnh

HTML

<div id="stage" class="preserve3d">
    <div id="contentShadow" class="preserve3d">
        <div id="scrollTopBrace" class="scrollTopBrace"><div class="scrollTopLabelWrapper"><div class="scrollTopLabel">scrollTop: <span id="scrollTopValue"></span></div></div></div>
        <div class="label">content</div>
        <div id="decreaseHeightButton">&#9650; -40px</div>
        <div id="increaseHeightButton">&#9660; +40px</div>
    </div>
    <div id="topLayerWrapper" class="topLayer">
        <div id="wrapper" class="wrapper"><div id="content"></div></div>
        <div class="label">wrapper</div>
        <div id="scrollLabel" class="scrollLabel">scrolled</div>
    </div>
</div>

CSS

#stage {
    position:absolute;
    top:180px;
    left:80px;
    font-size: 20px;
    /* for browsers not supporting 3d transforms fallback to this */
    -webkit-transform: rotate(-30deg) skew(10deg);
    -moz-transform: rotate(-30deg) skew(10deg);
    -ms-transform: rotate(-30deg) skew(10deg);
    transform: rotate(-30deg) skew(10deg);
    /* for browsers supporting 3d transforms */
    -webkit-transform: rotateX(45deg) rotateZ(-35deg);
    -moz-transform: rotateX(45deg) rotateZ(-35deg);
    -ms-transform: rotateX(45deg) rotateZ(-35deg);
    transform: rotateX(45deg) rotateZ(-35deg);
}
#topLayerWrapper {
    position: absolute;
    width: 200px;
    height: 200px;
}
#content {
    height: 300px;
}
#contentShadow {
    position:absolute;
    background-color: #999999;
}
#decreaseHeightButton, #increaseHeightButton {
    position: absolute;
    left: -104px;
    width: 100px;
    height: 38px;
    line-height: 38px;
    text-align: center;
    border: 1px solid #999999;
    background-color: #cccccc;
    cursor: pointer;
}
#decreaseHeightButton {
    bottom: 1px;
}
#increaseHeightButton {
    bottom: -41px;
}
.scrollTopBrace {
    position: absolute;
    top: 0;
    width: 10px;
    border: 2px solid #333333;
}
#scrollTopBrace {
    right: -15px;
    border-left-width: 0;
}
.scrollTopLabelWrapper {
    position: absolute;
    top: 50%;
}
.scrollTopLabel {
    position: absolute;
    white-space: nowrap;
}
#scrollTopBrace .scrollTopLabel {
    -webkit-transform: translate(20px, -50%);
    -moz-transform: translate(20px, -50%);
    -ms-transform: translate(20px, -50%);
    transform: translate(20px, -50%);
}
.wrapper {
    position: absolute;
    overflow: auto;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    box-shadow: 0 0 8px 4px #333333 inset;
    border: 2px solid #333333;
}
.label {
    position: absolute;
    right: 5px;
    bottom: 5px;
    /* chrome bug only */
    -webkit-transform: translateZ(1px);
}
.scrollLabel {
    position: absolute;
   ...

JavaScript

var content = document.getElementById("content"),
    decreaseHeightButton = document.getElementById("decreaseHeightButton"),
    increaseHeightButton = document.getElementById("increaseHeightButton"),
    
    wrapper = document.getElementById("wrapper"),
    scrollLabel = document.getElementById("scrollLabel"),
    contentShadow = document.getElementById("contentShadow"),
    scrollTopBrace = document.getElementById("scrollTopBrace"),
    scrollTopValue = document.getElementById("scrollTopValue"),
    scrollLabelHideTimeout = null;

function updateScrollTop() {
    contentShadow.style.top = -wrapper.scrollTop + "px";
    scrollTopBrace.style.height = wrapper.scrollTop + "px";
    scrollTopValue.textContent = wrapper.scrollTop + "px";
}

function updateContentShadowSize() {
    contentShadow.style.width = content.offsetWidth + "px";
    contentShadow.style.height = content.offsetHeight + "px";
}

function showScrollLabel() {
    scrollLabel.className = "scrollLabel show";
    window.clearTimeout(scrollLabelHideTimeout);
    scrollLabelHideTimeout = window.setTimeout(hideScrollLabel, 500);
}

function hideScrollLabel(e) {
    scrollLabel.className = "scrollLabel hide";
    scrollLabelHideTimeout = null;
}

updateContentShadowSize();

wrapper.scrollTop = wrapper.scrollHeight - wrapper.clientHeight;

updateScrollTop();

decreaseHeightButton.addEventListener("click", function() {
    content.style.height = (content.offsetHeight - 40) + "px";
    updateContentShadowSize();
    updateScrollTop();
}, false);

increaseHeightButton.addEventListener("click", function () {
    content.style.height = (content.offsetHeight + 40) + "px";
    updateContentShadowSize();
}, false);

wrapper.addEventListener("scroll", function() {
    updateScrollTop();
    showScrollLabel();
}, false);