js获取json文件内容随机抽取输出代码

所属分类: 网页特效-实用工具    2024-01-23 02:31:51

js获取json文件内容随机抽取输出代码 ie兼容6
反馈问题  查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

js获取json文件内容随机抽取输出代码(共4个文件)

    • 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(
    "quotes.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. 平台上所有素材资源,需注册登录会员方能正常下载。
2. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励20K币
3. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
4. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 实用工具

jquery页面右下角鼠标悬停滑动显示的在线客服特效代码

一款页面右侧浮动固定的在线客服代码,鼠标悬停于图标上触发动画滑动显示联系方式,可显示文本文字或图片。
  实用工具
 85  

jquery基于clipboard代码全选复制插件

Code Box Copy是一个常用的Prism语法高亮器插件,可自定义的复制按钮将代码框中的内容复制到剪贴板。
  实用工具
 90  

javascript在线计算器实用工具

一款响应式黑色风格计算器代码,可执行加法、减法、乘法和除法的计算。计算器的主要功能是输入计算公式和计算结果,非常的高效和实用。
  实用工具
 85  

jquery支持放大缩小的像素艺术图片生成器工具

这是一款模糊图片生成器实用工具,可将图片转换为像素化的图片,可通过拖动滑块来生成不同大小的预览图,自动生成CSS代码。
  实用工具
 63  

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

^_^ 还没有人评论,快来抢个沙发!
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论