限时优惠活动
亲爱的苦力吧用户,我们为了回馈新老用户一直以来的鼎力支持,即日起(2025-02-06至2025-03-06)凡是购买苦力吧VIP/充值K币的新老用户,都直接可获得买一送一的优惠馈赠。例如:购买一个月的VIP会员可直接获得两个月的VIP会员;充值100K币可直接获得200K币,以此类推!有任何疑问可联系在线客服,感谢各位用户对苦力吧素材的信任与厚爱,我们将一如既往的给大家上新更多优质的素材源码,祝大家开工大吉、工作顺利、心想事成。

js获取json文件数据动态随机输出显示特效代码

所属分类: 网页特效-其它&杂项    2023-12-19 11:19:51

js获取json文件数据动态随机输出显示特效代码 ie兼容6
 查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

js获取json文件数据动态随机输出显示特效代码(共4个文件)

    • data.json
    • index.html

使用方法

const projectName = "Random-quote-machine";

const colors = [
  "#16a085",
  "#27ae60",
  "#2c3e50",
  "#f39c12",
  "#e74c3c",
  "#9b59b6",
  "#FB6964",
  "#342224",
  "#472E32",
  "#BDBB99",
  "#77B1A9",
  "#73A857",
  "#ff0000",
  "#00ff00",
  "#0000ff",
  "#ffff00",
  "#ff00ff",
  "#00ffff",
  "#ff8800",
  "#ff0088",
  "#00ff88",
  "#88ff00",
  "#0088ff",
  "#8800ff",
];

let currentQuote = "";
let currentAuthor = "";
let quotesData = [];

function getQuotes() {
  return fetch(
    "https://kuli8.com/demo/js_quotes/data.json"
  )
    .then((response) => response.json())
    .then((data) => {
      quotesData = data.quotes;
    });
}

function getRandomQuote() {
  return quotesData[Math.floor(Math.random() * quotesData.length)];
}

function getQuote() {
  const randomQuote = getRandomQuote();

  currentQuote = randomQuote.quote;
  currentAuthor = randomQuote.author;

  const tweetQuoteLink = document.getElementById("tweet-quote");
  tweetQuoteLink.href =
    "https://twitter.com/intent/tweet?hashtags=quotes&related=freecodecamp&text=" +
    encodeURIComponent('"' + currentQuote + '" ' + currentAuthor);

  const tumblrQuoteLink = document.getElementById("tumblr-quote");
  tumblrQuoteLink.href =
    "https://www.tumblr.com/widgets/share/tool?posttype=quote&tags=quotes,freecodecamp&caption=" +
    encodeURIComponent(currentAuthor) +
    "&content=" +
    encodeURIComponent(currentQuote) +
    "&canonicalUrl=https%3A%2F%2Fwww.tumblr.com%2Fbuttons&shareSource=tumblr_share_button";

  const quoteText = document.querySelector(".quote-text");
  const textElement = document.getElementById("text");
  quoteText.style.opacity = 0;
  setTimeout(() => {
    quoteText.style.opacity = 1;
    textElement.textContent = randomQuote.quote;
  }, 500);

  const quoteAuthor = document.querySelector(".quote-author");
  const authorElement = document.getElementById("author");
  quoteAuthor.style.opacity = 0;
  setTimeout(() => {
    quoteAuthor.style.opacity = 1;
    authorElement.textContent = randomQuote.author;
  }, 500);

  const color = Math.floor(Math.random() * colors.length);
  const color2 = Math.floor(Math.random() * colors.length);
  document.documentElement.style.backgroundColor = colors[color];
  document.body.style.color = colors[color];
  const buttons = document.querySelectorAll(".button");
  buttons.forEach((button) => {
    button.style.backgroundColor = colors[color];
  });
}

document.addEventListener("DOMContentLoaded", () => {
  getQuotes().then(() => {
    getQuote();
  });

  const newQuoteButton = document.getElementById("new-quote");
  newQuoteButton.addEventListener("click", getQuote);
});

站长提示:
1. 苦力吧素材官方QQ群:950875342
2. 平台上所有素材资源,需注册登录会员方能正常下载。
3. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励2K币
4. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
5. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 其它&杂项

jquery延迟加载显示文本框数据插件

zParallel是一个延迟执行插件,它模拟在空闲时间并行执行函数。当页面所有元素加载完毕后,最后执行加载显示input文本框值。
  其它&杂项
 4269  0

jquery改变网页背景颜色切换

一款基于jquery可改变背景和字体颜色的拾色器插件,点击背景和颜色可以改变,用于自定义网站颜色皮肤。
  其它&杂项
 5117  0

jquery自动跟踪获取当前鼠标实时坐标插件

一款轻量级获取鼠标坐标脚本,用于跟踪鼠标移动事件,并使用JavaScriptMouseEvent API获取当前鼠标指针相对于窗口的位置(水平和垂直XY坐标)。
  其它&杂项
 6273  0

jquery支持拖动自定义节点的示例图插件

一款带svg箭头的示例流程图特效代码,支持鼠标拖动节点及设置文本文字。
  其它&杂项
 6309  0

评论数(2) 回复有机会获得K币 用户协议

    xiaoshenzhen0
    2023-12-28 16:21:44
    这是随机输出本地的json文件内容吗
    回复
    xiaoshenzhen0
    2023-12-27 14:03:39
    这个不错,刚好能用上😚
    回复
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论