Skip to content

Commit 0eb45d1

Browse files
committed
feat(PC): GIF 动图按顺序播放
1 parent 2c90fce commit 0eb45d1

File tree

6 files changed

+645
-3
lines changed

6 files changed

+645
-3
lines changed

packages/zhiyicx-plus-pc/resources/assets/web/css/feed.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ to {-webkit-transform:rotate(360deg);transform:rotate(360deg);}
6868
/* 多图片样式 */
6969
.image_box {position:relative;left:-1px;overflow:hidden;margin-bottom:2px;padding:0 2px;}
7070
.image_box .per_image {position:absolute;top:0;left:2px;width:100%;height:100%;object-fit:cover;}
71+
.image_box .gif_badge {position: absolute;bottom: 0;right: 0;background-color: #5dc8ab;color: #fff;font-size: 80%;padding: 1px 4px;}
72+
.image_box .long_badge {position: absolute;bottom: 0;right: 0;background-color: #c8a06c;color: #fff;font-size: 80%;padding: 1px 4px;}
73+
.image_box.playing .gif_badge{display: none;}
7174
.image_box:before {display:block;padding-top:100%;content:'';}
7275
.feed_item .feed_bottom {margin-top:20px;}
7376
.feed_item .feed_bottom .feed_datas {position:relative;clear:both;margin-top: 20px;}

packages/zhiyicx-plus-pc/resources/assets/web/js/common.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,6 +1840,60 @@ var repostable = {
18401840
},
18411841
}
18421842

1843+
function handleFile(blob, callback) {
1844+
var reader = new FileReader()
1845+
reader.onload = function(event) {
1846+
var arrayBuffer = reader.result
1847+
var gifInfo = gify.getInfo(arrayBuffer)
1848+
callback(gifInfo)
1849+
}
1850+
reader.readAsArrayBuffer(blob)
1851+
}
1852+
1853+
// 播放 GIF 图
1854+
if ('getContext' in document.createElement('canvas')) {
1855+
HTMLImageElement.prototype.play = function() {
1856+
var that = this
1857+
that.parentElement.classList.add('playing')
1858+
if (that.dataset.blobURL) {
1859+
that.src = that.dataset.blobURL
1860+
gifInfo.timer = setTimeout(function() {
1861+
that.stop()
1862+
gifInfo.currentIndex++
1863+
}, that.dataset.gifDuration)
1864+
return
1865+
}
1866+
// 从远程获取 GIF blob 对象
1867+
axios.get(that.dataset.originalGif, {
1868+
responseType: 'blob'
1869+
}).then(function(res) {
1870+
var blob = res.data
1871+
1872+
// 加载图片
1873+
var blobURL = window.URL.createObjectURL(blob);
1874+
that.src = blobURL
1875+
that.dataset.blobURL = blobURL
1876+
1877+
// 解析 GIF 信息 (via gify)
1878+
handleFile(blob, function (info) {
1879+
console.log(info);
1880+
// 读取 GIF 持续时间
1881+
that.dataset.gifDuration = info.durationChrome
1882+
// 停止播放
1883+
gifInfo.timer = setTimeout(function() {
1884+
that.stop()
1885+
gifInfo.currentIndex++ // 触发索引变更 播放下一个 GIF
1886+
}, that.dataset.gifDuration)
1887+
})
1888+
})
1889+
};
1890+
HTMLImageElement.prototype.stop = function() {
1891+
clearTimeout(gifInfo.timer)
1892+
this.parentElement.classList.remove('playing')
1893+
this.src = this.dataset.original
1894+
};
1895+
}
1896+
18431897
$(function() {
18441898

18451899
// 获取我的好友 用于全局at弹框显示默认内容

packages/zhiyicx-plus-pc/resources/assets/web/js/gify.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)