Git Product home page Git Product logo

randomimgs's Introduction

randomimgs

通过前端js跳转实现随机图片API

思路

  • Github page可以上传静态代码
  • js可以随机读取url文件,并在js数组中用random函数实现随机返回url部分内容
  • 使用js跳转功能,当用户通过Github Page点击到前端页面时,背后的Js将根据随机函数,随机跳转到图片的url
  • 图片以第三方图床的形式保存
  • TG图床
  • Github Page使用cloudflare实现加速同步部署

代码实现

2023/03/16/l51Lx
2023/03/14/fLKzb
2023/03/09/Y0iNK
2023/03/09/Y0hOs
2023/03/09/Y0zdB
2023/03/09/Y0kTl
2023/03/09/Y0qLD
2023/03/09/YSCTr
2023/03/09/YS2LU
2023/03/09/YSj7p

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="author" content="瓶子的跋涉">
    <title>Random photo API</title>
    <script src="index.js" type="text/javascript" ></script>
</head>
<body>
</body>
</html>

//当打开html自动调用
window.onload = function() {
    //调用随机函数
    loadRandomImage();
}
//函数
function loadRandomImage() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        //始终进行
        if (xhttp.status === 200) {
            //将所有图片url放入imageUrls当中
            var imageUrls = this.responseText.split("\n");
            //实现随机访问
            var randomIndex = Math.floor(Math.random() * imageUrls.length);
            //实现额面跳转
            window.location.href="https://i.imgtg.com/" + imageUrls[randomIndex] + ".jpg";
        }
        else{
            console.log("图片文件状态异常,可联系[email protected]")
        }
    };
    //打开文件
    xhttp.open("GET", "photos_urls.txt", true);
    xhttp.send();
}

randomimgs's People

Contributors

foryouos avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.