javascript - 将 onload 函数与 onresize 函数连接
问题描述
我想根据屏幕大小缩短一些 API 标题。所以我有一个 onload 函数来获取和显示 API 的内容,但我希望一个 onresize 函数在屏幕宽度的大小发生变化的任何时候都可以生效。
window.onload = function displayNews(width){
fetch('some api url')
.then(res => {
return (res.json())
})
.then(function(res) {
let postsArr = res.data.children;
for (let i = 0; i < postsArr.length; i++) {
let currPost = postsArr[i].data;
let currPostTitle = currPost.title;
if (width > 1525) {
currPostTitle = currPostTitle.substring(0, 100) + '…';
} else if (width < 1525 && width > 1090) {
currPostTitle = currPostTitle.substring(0, 20) + '…';
} else if (width < 1090) {
currPostTitle = currPostTitle.substring(0, 200) + '…';
}
let markup =
`
//(Some API display)
然后我创建了将宽度变量传递给 onload 函数的 onresize 函数
window.onresize = function windowWidth() {
let width = $(window).width();
displayNews(width);
}
但它不起作用,我不知道在哪里放置 onresize 函数。我试图将它放在 onload 中,但也许我犯了一个错误。你怎么看?
解决方案
推荐阅读
- java - 包含敏感数据的可序列化类
- jquery - 日期选择器格式不显示正确的日期 mvc 5
- c++ - 在 2D 矢量数组中的特定标题下添加数据
- php - 使用jquery ajax上传文件然后得到json格式响应但不能显示json数据
- r - 删除 R 中的空白行和关联 ID
- ruby-on-rails - Shrine - 无法再次读取附件
- bootstrap-4 - DateTimePicker 引导程序显示左侧站点
- vega - 如何在 VegaLite 中隐藏尺寸图例并按区域标准化尺寸?
- visual-studio - Visual Studio Community 2019 找不到我的项目模板
- r - 在 R 中读取以 UTC+偏移量(无 DST)表示的日期时间