这个答案建立在 Sourabh 的基础上,他在 https://codepen.io/hoanghals/pen/dZrWLZ 上指出了一个 HTML/CSS/JavaScript 组合来完成这项工作。我尝试了这个,并制作了一个完整的网页,包括我在我的网站上尝试过的 CSS 和 JavaScript。由于 CodePens 有消失的习惯,我决定在这里展示一下。我还展示了一个简化的精简版,以展示一个人需要做的最低限度的工作。
我还必须注意一件事。上面链接中的代码(其 JavaScript Sourabh 复制了该代码)引用了 JavaScript 构造函数 SuperGif() 。我认为 Sourabh 没有解释这一点,CodePen 也没有。一个简单的搜索表明它是在buzzfeed /
libgif-js ,可以从https://github.com/buzzfeed/libgif-js#readme 下载。查找下方红色箭头所指的控件,然后单击绿色的“代码”按钮。 (注意,你不会看到红色箭头:那是我告诉你在哪里看。)
将弹出一个菜单,提供各种选项,包括下载 zip 文件。下载它,并将其解压缩到您的 HTML 目录或其子目录中。
接下来,我将展示我制作的两个页面。第一个是从 CodePen 派生的。第二个被剥离了它的基本要素,并显示了使用 SuperGif 所需的最低限度。
这是第一页的完整 HTML、CSS 和 JavaScript。 HTML 的头部是指向 libgif.js 的链接,这是您需要的 zip 文件中的文件。然后,HTML 的正文以一些关于猫图片的文本开头,然后是一个指向动画猫 GIF 的链接,地址为 https://media.giphy.com/media/Byana3FscAMGQ/giphy.gif。
然后继续使用一些 CSS。 CodePen 使用 SCSS,任何不知道的人都必须将其预处理为 CSS。我已经这样做了,所以下面的代码是真正的 CSS。
最后是 JavaScript。
<html>
<head>
<script src="libgif-js-master/libgif.js"></script>
</head>
<body>
<div style="width: 600px; margin: auto; text-align: center; font-family: arial">
<p>
And so, the unwritten law of the internet, that any
experiment involving video/images must involve cats in
one way or another, reared its head again. When would
the internet's fascination with cats come to an end?
Never. The answer is "Never".
</p>
<img src='https://media.giphy.com/media/Byana3FscAMGQ/giphy.gif' class='gif' />
</div>
<style>
img.gif {
visibility: hidden;
}
.jsgif {
position: relative;
}
.gifcontrol {
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
cursor: pointer;
transition: background 0.25s ease-in-out;
z-index: 100;
}
.gifcontrol:after {
transition: background 0.25s ease-in-out;
position: absolute;
content: "";
display: block;
left: calc(50% - 25px);
top: calc(50% - 25px);
}
.gifcontrol.loading {
background: rgba(255, 255, 255, 0.75);
}
.gifcontrol.loading:after {
background: #FF9900;
width: 50px;
height: 50px;
border-radius: 50px;
}
.gifcontrol.playing {
/* Only show the 'stop' button on hover */
}
.gifcontrol.playing:after {
opacity: 0;
transition: opacity 0.25s ease-in-out;
border-left: 20px solid #FF9900;
border-right: 20px solid #FF9900;
width: 50px;
height: 50px;
box-sizing: border-box;
}
.gifcontrol.playing:hover:after {
opacity: 1;
}
.gifcontrol.paused {
background: rgba(255, 255, 255, 0.5);
}
.gifcontrol.paused:after {
width: 0;
height: 0;
border-style: solid;
border-width: 25px 0 25px 50px;
border-color: transparent transparent transparent #ff9900;
}
</style>
<script>
var gifElements = document.querySelectorAll('img.gif');
for(var e in gifElements) {
var element = gifElements[e];
if(element.nodeName == 'IMG') {
var supergif = new SuperGif({
gif: element,
progressbar_height: 0,
auto_play: false,
});
var controlElement = document.createElement("div");
controlElement.className = "gifcontrol loading g"+e;
supergif.load((function(controlElement) {
controlElement.className = "gifcontrol paused";
var playing = false;
controlElement.addEventListener("click", function(){
if(playing) {
this.pause();
playing = false;
controlElement.className = "gifcontrol paused";
} else {
this.play();
playing = true;
controlElement.className = "gifcontrol playing";
}
}.bind(this, controlElement));
}.bind(supergif))(controlElement));
var canvas = supergif.get_canvas();
controlElement.style.width = canvas.width+"px";
controlElement.style.height = canvas.height+"px";
controlElement.style.left = canvas.offsetLeft+"px";
var containerElement = canvas.parentNode;
containerElement.appendChild(controlElement);
}
}
</script>
</body>
</html>
当我将页面放到我的网站上并显示它时,顶部看起来是这样的:
当我按下粉色按钮时,页面变成了这个,GIF 开始动画。 (猫舔着水龙头里掉下来的水。)
最后,这是第二个简单的页面。与第一个不同,它没有花哨的播放/暂停控件来改变形状:它只有两个按钮。代码唯一不需要做的事情就是禁用不相关的按钮,并在按钮之间插入一些空格。
<html>
<head>
<script src="libgif-js-master/libgif.js"></script>
</head>
<body>
<button type="button" onclick="play()"
id="play_button"
style="margin-right:9px;"
>
Play
</button>
<button type="button" onclick="pause()"
id="pause_button"
>
Pause
</button>
<img src="https://media.giphy.com/media/Byana3FscAMGQ/giphy.gif"
id="gif"
/>
<script>
var gif_element = document.getElementById( "gif" );
var supergif = new SuperGif( {
gif: gif_element,
progressbar_height: 0,
auto_play: false
} );
supergif.load();
function play()
{
var play_button = document.getElementById( "play_button" );
play_button.disabled = true;
var pause_button = document.getElementById( "pause_button" );
pause_button.disabled = false;
supergif.play();
}
function pause()
{
var play_button = document.getElementById( "play_button" );
play_button.disabled = false;
var pause_button = document.getElementById( "pause_button" );
pause_button.disabled = true;
supergif.pause();
}
pause_button.disabled = true;
</script>
</body>
</html>
这个,加上 libgif-js 中的 example.html 文件,应该足以让任何人开始。