import math time_per_img = 7.0 time_per_trans = 2.0 num_imgs = 4 anim_block = '''/* Animations */ {keyframes} .side .usertext-body .md > ul:last-of-type a {{ background:url(%%potw%%); opacity: 0; z-index: 0; -webkit-animation: fadeInOut1 {time}s linear infinite; animation: fadeInOut1 {time}s linear infinite; }} {imgblocks}''' total_time = (time_per_img + time_per_trans) * num_imgs time = int(total_time) time_perc = 100 / total_time on_perc = time_per_img * time_perc trans_perc = time_per_trans * time_perc def safeTime(t): if t < 0: t = 100 + t if t > 100: t = t - 100 return t def calcSlot(i): on = safeTime(i * (on_perc + trans_perc)) startOff = safeTime(on + on_perc) startOn = safeTime(on - trans_perc) startZOff = safeTime(startOn - 1) off = safeTime(startOff + trans_perc) endZOff = safeTime(off + 1) order = [math.floor(a) for a in (startZOff, startOn, on)] + \ [math.ceil(a) for a in (startOff, off, endZOff)] commands = ['z-index:0', 'opacity:0;z-index:1', 'opacity:1', 'opacity:1', 'opacity:0;z-index:1', 'z-index:0'] d = dict((pos, commands[i]) for i, pos in enumerate(order)) if 0 in d and 100 not in d: d[100] = d[0] elif 100 in d and 0 not in d: d[0] = d[100] return sorted(d.items(), key=lambda a: a[0]) keyframes = '' imgblocks = '' for i in range(num_imgs): percs = ' '.join(['{}%{{{}}}'.format(int(a), b) for a, b in calcSlot(i)]) for prefix in ('', 'webkit', 'moz', 'o'): prefix = '-{}-'.format(prefix) if '' != prefix else '' keyframes += '''@{prefix}keyframes fadeInOut{animNum} {{ {percs} }} '''.format(prefix=prefix, animNum=i + 1, percs=percs) if i > 0: imgblocks += '''.side .usertext-body .md > ul:last-of-type li:nth-of-type({i}) a{{ animation-name: fadeInOut{i}; -webkit-animation-name: fadeInOut{i}; background:url(%%potw{i}%%); }} '''.format(i=i+1) print anim_block.format(keyframes=keyframes, time=time, imgblocks=imgblocks) #
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question