从头开始动画不能与负值一起使用?

时间:2018-10-21 21:03:29

标签: javascript animation

编辑

解决了。请参阅下面的帖子。

/编辑结束

编辑

与上面建议的主题无关,并且不能通过将vh作为最高值的度量来解决,而是如本小提琴所示(如此处的第一条评论所建议):http://jsfiddle.net/wm02ovx8/3/。 div仍然没有动画,而是在眨眼间弹出。

/编辑结束

我正在尝试从上到下为抽屉div设置动画。只要从头开始,动画就可以正常工作(“顶部” css属性值大于-1),但是由于我在calc(-100% + 30px)属性上具有一个负值top它只是“弹出”。我在这里想念什么?

[Fiddle] (要打开菜单,请按右侧的索引词)

HTML:

<div id="closeIndexLayer"></div>
<div id="alignLinks">
    <div id="anchorLinks"></div>
</div>

<div id="indexMenu">Index</div>

CSS:

div#alignLinks {
    position: fixed;
    display: flex;
    align-items: baseline;
    justify-content: flex-start;
    flex-flow: wrap;
    left: 30px;
    top: calc(-100% + 30px);
    background: rgba(255,0,0,.60);
    /* transform: translateX(-50%); */
    height: calc(100% - 30px);
    border-bottom-left-radius: 30px;
    border-bottom-right-radius: 30px;
    z-index: 250 !important;
    min-width: 425px;
    max-width: 425px;
    box-shadow: 0 19px 38px rgba(0,0,0,0.30), 0 15px 12px rgba(0,0,0,0.22);
}

div#anchorLinks {
    display: flex;
    /* position: fixed; */
    flex-flow: wrap;
    height: auto;
    /* position: absolute; */
    /* background: rgba(0,0,0,.5); */
    top: 0px;
    right: 0px;
    z-index: 250;
}

div#closeIndexLayer {
    position: fixed;
    display:none;
    top: 0;
    left: 0;
    box-sizing: border-box;
    width: 100%;
    height: 100%;
    background: green;
    z-index: 250;
}

a.anchorLink {
    width: auto;
    background: hsla(210, 11%, 8%, 1);
    border-radius: 3px;
    text-decoration: none;
    font-size: 2rem;
    margin-top: 16px;
    margin-bottom: 2px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
    -webkit-transition: all 140ms ease-in-out;
    -moz-transition: all 140ms ease-in-out;
    -ms-transition: all 140ms ease-in-out;
    -o-transition: all 140ms ease-in-out;
    transition: all 140ms ease-in-out;
}

#indexMenu {
        position: fixed;
        display: block;
        z-index: 249;
        top: 50%;
        height: 18px;
        right: -18px;
        padding: 0px 6px 0px 6px;
        transform: translateY(-50%) rotate(270deg);
        background: rgba(255,255,255,.15);
        border-radius: 5px 5px 0px 0px;
        font-family: 'Nunito Sans', sans-serif;
        font-size: 10px;
        box-shadow: 0px 1px 2px rgba(255,255,255,.12), 0px 2px 3px rgba(255,255,255,.24);
    cursor: pointer;
    }

Javascript:

function showIndex() {
    var elem = document.getElementById("alignLinks");
    thestyle = window.getComputedStyle(elem),
    thetop = thestyle.getPropertyValue('top');

    var pos = 0;
    var id = setInterval(frame, 5);
    function frame() {
        if (pos <= 0) {
            pos++;
            elem.style.top = pos + 'px';
        } else {
            clearInterval(id);
        }
    }
}

function hideIndex() {
    var elem = document.getElementById("alignLinks");
    thestyle = window.getComputedStyle(elem),
        thetop = thestyle.getPropertyValue('top');

    var pos = 0;
    var id = setInterval(frame, 5);
    function frame() {
        if (pos >= 0) {
            pos--;
            elem.style.top = pos + 'px';
        } else {
            clearInterval(id);
        }
    }
}

document.getElementById("indexMenu").addEventListener("click", function(){
    var closeIndexLayer = document.getElementById("closeIndexLayer");

    closeIndexLayer.style.display = "block";
    showIndex();
});

document.getElementById("closeIndexLayer").addEventListener("click", function(){
    var closeIndexLayer = document.getElementById("closeIndexLayer");

    closeIndexLayer.style.display = "none";
    hideIndex();
});
//</editor-fold>

1 个答案:

答案 0 :(得分:0)

我以另一种方式解决了它,并且以一种我更熟悉的方式解决了它。我发现对我有用的方法将一个类添加到元素,并且该类具有动画属性。然后,如果要进行动画处理,只需将动画属性添加到正在动画的元素(在本例中为ID #alignLinks)。

[Fiddle]

Javascript:

let drawer = document.getElementById("alignLinks");


document.getElementById('indexMenu').onclick = function() {
  if(this.innerHTML === 'Index') 
  { 
    this.innerHTML = 'Close';
    drawer.classList.add('topAnimDown');
  } else {
    this.innerHTML = 'Index';
        var computedStyle = window.getComputedStyle(drawer),
    topProp = computedStyle.getPropertyValue('top');
    drawer.style.topProp = topProp;
    drawer.classList.remove('topAnimDown');
  }  
}

HTML

<div id="closeIndexLayer"></div>
<div id="alignLinks">
    <div id="anchorLinks"></div>
</div>

<div id="indexMenu">Index</div>

CSS

body {
  background: hotpink;
}

div#alignLinks {
    position: fixed;
    display: flex;
    align-items: baseline;
    justify-content: flex-start;
    flex-flow: wrap;
    left: 30px;
    top: calc(-100% + 30px);
    background: rgba(255,0,0,.60);
    /* transform: translateX(-50%); */
    height: calc(100% - 30px);
    border-bottom-left-radius: 30px;
    border-bottom-right-radius: 30px;
    z-index: 250 !important;
    min-width: 425px;
    max-width: 425px;
    box-shadow: 0 19px 38px rgba(0,0,0,0.30), 0 15px 12px rgba(0,0,0,0.22);
    -webkit-transition: 3s ease-in-out;
  -moz-transition: 3s ease-in-out;
  -ms-transition: 3s ease-in-out;
  -o-transition: 3s ease-in-out;
  transition: 3s ease-in-out;
}

.topAnimDown {
  -webkit-transition: 3s ease-in-out;
  -moz-transition: 3s ease-in-out;
  -ms-transition: 3s ease-in-out;
  -o-transition: 3s ease-in-out;
  transition: 3s ease-in-out;
  top: 0 !important;
}

div#anchorLinks {
    display: flex;
    /* position: fixed; */
    flex-flow: wrap;
    height: auto;
    /* position: absolute; */
    /* background: rgba(0,0,0,.5); */
    top: 0px;
    right: 0px;
    z-index: 250;
}

div#closeIndexLayer {
    position: fixed;
    display:none;
    top: 0;
    left: 0;
    box-sizing: border-box;
    width: 100%;
    height: 100%;
    background: green;
    z-index: 250;
}

a.anchorLink {
    width: auto;
    background: hsla(210, 11%, 8%, 1);
    border-radius: 3px;
    text-decoration: none;
    font-size: 2rem;
    margin-top: 16px;
    margin-bottom: 2px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
    -webkit-transition: all 140ms ease-in-out;
    -moz-transition: all 140ms ease-in-out;
    -ms-transition: all 140ms ease-in-out;
    -o-transition: all 140ms ease-in-out;
    transition: all 140ms ease-in-out;
}

#indexMenu {
        position: fixed;
        display: block;
        z-index: 249;
        top: 50%;
        height: 18px;
        right: -18px;
        padding: 0px 6px 0px 6px;
        transform: translateY(-50%) rotate(270deg);
        background: rgba(255,255,255,.15);
        border-radius: 5px 5px 0px 0px;
        font-family: 'Nunito Sans', sans-serif;
        font-size: 24px;
        box-shadow: 0px 1px 2px rgba(255,255,255,.12), 0px 2px 3px rgba(255,255,255,.24);
    cursor: pointer;
    }