简体   繁体   English

通过使用Java删除类来触发CSS转换

[英]Triggering a CSS transition by removing a class with Javascript

I'm having trouble figuring out why my CSS transition isn't firing when I programmatically remove a class from an element. 我无法弄清楚为什么我以编程方式从元素中删除类时为什么我的CSS转换没有触发。

Essentially, I am trying to create a progressively enhanced, infinitely-scrolling carousel using pure Javascript. 本质上,我试图使用纯Javascript创建一个逐步增强的,无限滚动的轮播。

The basic idea is that as the user clicks to scroll either left or right, the first or last element is plucked from the parent ul element, and either prepended or appended to the proper location, depending on the scroll-direction of the user's click. 其基本思想是,当用户单击以向左或向右滚动时,第一个或最后一个元素将从父ul元素中拔出,并根据用户单击的滚动方向在其前或后添加到正确的位置。

Here is the relevant code: 以下是相关代码:

HTML: HTML:

<div id="scroller">
    <ul>
        <li>
            <a>
                <img src="...">
            </a>
        </li>
        ..... more li elements
        <li>
            <a>
                <img src="...">
            </a>
        </li>
    </ul>   
</div>

CSS: CSS:

#scroller {
    position: absolute;
    left: 0em;
    height: 8em;
    width: 400em;
}
#scroller ul {
    list-style: none;
}
#scroller ul li {
    overflow: hidden;
    float: left;
    height: 8em;
    width: 8em;
    transition: width 1s ease;
}
#scroller ul li.hide {
    width: 0em;
}
#scroller ul li a img {
    width: 8em;
}

JS (scroll right click event, for example): JS(例如,滚动右键事件):

/** Remove the element from the end of the list, add the hide class */
var node = this.list.removeChild(this.items[(this.items.length - 1)]);
/** Add the hide class to the node */
node.className += ' hide';
/** Insert the node at the beginning of the scroller */
this.list.insertBefore(node, this.items[0]);
/** Remove the hide class to trigger the transition animation */
node.className = node.className.replace('hide', '');

Everything is working well, in terms of the items being shifted around the ul correctly, so that is not the problem. 一切都运行良好,就可以正确地在ul周围移动项目而言,所以这不是问题。

The issue is that the CSS transition is not being applied when the width of the li elements are changed by removing the "hide" class. 问题是,通过删除“ hide”类更改li元素的宽度时,未应用CSS过渡。

I had hoped to create a smooth scrolling effect in browsers than can support the CSS transition. 我曾希望在浏览器中创建一个平滑的滚动效果,而不是支持CSS过渡。

Thanks in advance for not suggesting that I use a JS library! 在此先感谢您不建议我使用JS库! :) :)

Use a combo of setTimeout and the transitionend event. 使用setTimeouttransitionend事件的组合。

Look here for more info on transitionend : CSS3 transition events 在此处查看有关transitionend更多信息: CSS3过渡事件

 /** Remove the element from the end of the list, add the hide class */ one = document.getElementById('one'); two = document.getElementById('two'); list = document.getElementById('list'); /** Add the hide class to the node */ two.addEventListener('transitionend', end, false); setTimeout(function(){ two.className += ' hide'; }, 0) function end(){ /** Insert the node at the beginning of the scroller */ list.insertBefore(two, one); /** Remove the hide class to trigger the transition animation */ setTimeout(function(){ two.className = two.className.replace('hide', ''); }, 0) } 
 #scroller { position: absolute; left: 0em; height: 8em; width: 400em; } #scroller ul { list-style: none; } #scroller ul li { overflow: hidden; float: left; height: 8em; width: 8em; transition: width 1s ease; } #scroller ul li.hide { width: 0em; } #scroller ul li a { width: 8em; background-color:red; } 
 <div id="scroller"> <ul id="list"> <li id="one"> <a> One </a> </li> <li id="two"> <a> Two </a> </li> </ul> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM