一个简单的头条新闻响应式(调用聚合数据api)

聚合数据API申请链接:https://www.juhe.cn/docs/api/id/235

架在服务器上。 地址可访问:http://106.13.193.149:8081/

后端使用的是nodejs(express+superagent)

直接上代码:

文件目录:

**app.js**

```

//superagent是一个HTTP库

这里之所以用superagent,也是因为前端页面如果直接掉聚合数据api 会出现跨域。

const superagent= require('superagent');

var express = require('express');

var app = express();

//解决跨域

app.all('*', function (req, res, next) {

    res.header('Access-Control-Allow-Origin', '*');

    //Access-Control-Allow-Headers ,可根据浏览器的F12查看,把对应的粘贴在这里就行

    res.header('Access-Control-Allow-Headers', 'Content-Type');

    res.header('Access-Control-Allow-Methods', '*');

    res.header('Content-Type', 'application/json;charset=utf-8');

    next();

  });

//静态资源路径

app.use('/public', express.static('public'));

//首页

app.get('/', function (req, res) {

    res.sendFile( __dirname + "/" + "toutiao.html" );

})

//首页会调用此接口,携带参数type 可返回不同的新闻类型

app.get('/get_toutiao',(req,res)=>{

  console.log(req.query.type)

  superagent.get(`http://v.juhe.cn/toutiao/index?key=58ff2000704bbf8e31ce031c1b555a33&type=${req.query.type}`).end((err, reso) => {

    // console.log(reso);

    //返回的是字符,需要前端页面转换为对象

    res.jsonp({

      data:reso

    })

  });

})

var server = app.listen(8081, function () {

  var host = server.address().address

  var port = server.address().port

  console.log("应用实例,访问地址为 http://%s:%s", host, port)

})

```

**toutiao.html**

```

<!DOCTYPE html>

<html>

<head>

  <meta charset="UTF-8">

  <!-- 引入样式 -->

  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">

  <link rel="stylesheet" href="./public/toutiao.css">

<title>每日头条</title>

</head>

<body>

  <div id="app">

    <div class="content">

        <div class="head">

            <div :class="{'dynamic':mark == 'top'}" class="item" @click="get_toutiao('top')">推荐</div>

            <div :class="{'dynamic':mark == 'shehui'}" class="item" @click="get_toutiao('shehui')">社会</div>

            <div :class="{'dynamic':mark == 'guonei'}" class="item" @click="get_toutiao('guonei')">国内</div>

            <div :class="{'dynamic':mark == 'guoji'}" class="item" @click="get_toutiao('guoji')">国际</div>

            <div :class="{'dynamic':mark == 'yule'}" class="item" @click="get_toutiao('yule')">娱乐</div>

            <div :class="{'dynamic':mark == 'tiyu'}" class="item" @click="get_toutiao('tiyu')">体育</div>

            <div :class="{'dynamic':mark == 'junshi'}" class="item" @click="get_toutiao('junshi')">军事</div>

            <div :class="{'dynamic':mark == 'keji'}" class="item" @click="get_toutiao('keji')">科技</div>

            <div :class="{'dynamic':mark == 'caijing'}" class="item" @click="get_toutiao('caijing')">财经</div>

            <div :class="{'dynamic':mark == 'shishang'}" class="item" @click="get_toutiao('shishang')">时尚</div>

        </div>

        <div class="body">

            <div class="item" v-for="item in items">

                <div class="title" :class="{left:true}"><a style="font-size:22px" :href="item.url" target="view_window">{{item.title}}</a></div>

                <div class="imgs" >

                    ![](item.thumbnail_pic_s)

                    ![](item.thumbnail_pic_s02)

                    ![](item.thumbnail_pic_s03)

                </div>

                <div class="date"><span style="color:#F56C6C;padding-right: 15px;">{{item.author_name}}</span><span>{{item.date}}</span></div>

            </div>

            <div class="bottom">

                <span>已经到底了</span>

            </div>

        </div>

    </div>

  </div>

</body>

  <!-- 先引入 Vue -->

  <script src="https://unpkg.com/vue/dist/vue.js"></script>

  <!-- 引入组件库 -->

  <script src="https://unpkg.com/element-ui/lib/index.js"></script>

  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

  <script>

    new Vue({

      el: '#app',

      data: function() {

        return {

            items: [],

            mark:'top'

        }

      },

      methods:{

        get_toutiao(type) {

            this.mark = type;

            axios.get('/get_toutiao', {

                params: {

                type: type

                }

            })

            .then((res)=>{

                var list = JSON.parse(res.data.data.text);

                console.log(list.result.data)

                this.items = list.result.data;

            })

            .catch((err)=>{

                console.log(err)

            });

        },

        scrollToTop () {

            const that = this

            let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop

            that.scrollTop = scrollTop

            if (that.scrollTop > 60) {

            that.btnFlag = true

            } else {

            that.btnFlag = false

            }

        }

      },

      mounted() {

          this.get_toutiao();

          window.addEventListener('scroll', this.scrollToTop)

      }

    })

  </script>

</html>

```

**public/toutiao.css**

```

body{

    margin: 0;

    padding: 0;

    color: black

}

a{text-decoration:none}

a:link {color: #000} /* 未访问时的状态 */

a:visited {color: #000} /* 已访问过的状态 */

a:hover {color: #F56C6C;text-decoration:underline} /* 鼠标移动到链接上时的状态 */

a:active {color: #F56C6C} /* 鼠标按下去时的状态 */

.content{

    width: 100%;

    /* height: 100vh; */

    margin: auto;

    padding-top: 75px;

}

.head{

    width: 100%;

    height: 50px;

    position: fixed;

    top: 0;

    z-index: 100;

    display: flex;

    justify-content: space-between;

    align-items: center;

    background-color: #409EFF;

    font-size: 18px

}

.head >.item{

    width: 8%;

    height: 100%;

    display: flex;

    justify-content: center;

    align-items: center;

    cursor: pointer;   

}

.dynamic{

    background-color: #F56C6C;

    color:#ffffff

}

.head >.item:hover{

    color: #F56C6C

}

.body{

    /* margin-top: 105px; */

    width: 65%;

    margin: auto;

    display: flex;

    flex-direction: column;

    justify-content: flex-start;

    align-items: center

}

.body>.item{

    width: 100%;

    /* height: 150px; */

    border-bottom: 1px solid #DCDFE6;

    display: flex;

    flex-direction: column;

    justify-content: space-between;

    margin-top: 25px;

}

.body >.item>.imgs{

    width: 100%;

    display: flex;

    justify-content: space-between;

    align-items: center;

    margin-top: 10px;

    margin-bottom: 7px;

}

.body>.bottom{

    width: 100%;

    height: 152px;

    display: flex;

    justify-content: center;

    align-items: center

}

```

### 效果图

————————————————

版权声明:本文为CSDN博主「BYh_blog」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/byhage1/article/details/99734445

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 222,000评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,745评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,561评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,782评论 1 298
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,798评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,394评论 1 310
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,952评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,852评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,409评论 1 318
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,483评论 3 341
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,615评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,303评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,979评论 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,470评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,571评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 49,041评论 3 377
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,630评论 2 359

推荐阅读更多精彩内容