js仿微博动态栏功能

 更新时间:2017年02月22日 11:04:00   作者:漫步未来  
本文主要介绍了js仿微博动态栏功能的示例代码,具有很好的参考价值,下面跟着小编一起来看下吧

知识有限,目前只写了发布动态的功能,没有写兼容,后面再慢慢完善。嘿嘿

效果图:

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>微博</title>
 <style>
  body, p, img, ul, li { margin: 0; padding: 0; }
  body { background-image: url("http://cdn.attach.qdfuns.com/notes/pics/201612/07/212742f92tpe2wve095ttp.jpg");}
  li { list-style: none; }
  .box {
   padding-top: 20px;
   margin: 20px auto;
   width: 620px;
   height: 180px;
   position: relative;
   background-color: #ffffff;
   text-align: center;
  }
  .box label {
   font: 18px/18px "微软雅黑";
   color: #cccccc;
   position: absolute;
   top: 60px;
   left: 40px;
   cursor: text;
  }
  .H {
   float: left;
   padding: 5px 0 5px 10px;
   color: #426B80;
   font: 400 16px/16px "宋体";
  }
  textarea {
   padding: 5px;
   border-color: #CCCCCC;
   width: 580px;
   height: 80px;
   resize: none;
   outline:none;
   font: 400 18px "微软雅黑";
   color: #333333;
  }
  #button {
   width: 80px;
   height: 34px;
   display: block;
   background-color: #FFC09F;
   position: absolute;
   top: 148px;
   right: 14px;
   text-align: center;
   line-height: 34px;
   color: #ffffff;
   cursor: pointer;
   /*F7671D*/
  }
  .dynamic {
   text-align: left;
   padding: 10px 10px;
   width: 580px;
   height: 100%;
  }
  .user {
   position: relative;
  }
  .user img {
   border: solid 1px #CCCCCC;
   vertical-align: top;
  }
  .user .name {
   display: block;
   position: absolute;
   top: 8px;
   left: 60px;
   font: 600 18px/18px "微软雅黑";
  }
  .time{
   display: block;
   position: absolute;
   top: 55px;
   left: 80px;
   font: 500 10px/10px "微软雅黑";
  }
  .dynamic .user {
   margin: 10px 5px 0 10px;
  }
  .dynamic .list {
   font: 500 16px/16px "微软雅黑";
   padding-left: 70px;
  }
 </style>
 <script>
  window.onload = function () {
   //获取当前时间函数
   function time() {
    var maydate = new Date();
    var Time = maydate.getFullYear() + "-" + (maydate.getMonth()+1) +"-"+maydate.getDate()+" "+maydate.getHours()+":"+maydate.getMinutes();
    return Time;
   }
   //获取ID函数
   function $(id) {return document.getElementById(id);}
   //创建节点函数
   function nweChild(id,text) {
    //获得节点ID
    var parent = $(id);
    //获得body所有的孩子
    var child = parent.parentNode.children;
    //克隆当前节点和其所有子节点
    var newNode = parent.cloneNode(true);
    //给当前节点的父节点插入克隆的节点
    parent.parentNode.insertBefore(newNode,child[1]);
    //插入的克隆节点更换ID
    child[1].id = id + (child.length - 2);
    //改变时间
    //获取当前id节点的所有孩子
    var idChild = child[1].children;
    idChild[0].innerHTML = time();
    idChild[2].innerHTML = $("text").value;
   }
   //获得焦点改变边框颜色和恢复默认字体颜色
   $("text").onfocus = function () {
    this.style.borderColor = "#FA7D3C";
    this.style.color = "#333333";
   }
   //失去焦点恢复默然边框颜色,改变字体颜色
   $("text").onblur = function () {
    this.style.borderColor = "#CCCCCC";
    this.style.color = "#CCCCCC";
   }
   //监听输入事件
   $("text").oninput = function () {
    if($("text").value != ""){
     //隐藏提示文本
     $("txt").style.display = "none";
     //文本域不为空 按钮可点击
     $("button").style.backgroundColor = "#FF8140";
    }else{
     //显示提示文本
     $("txt").style.display = "block";
     //文本域为空 按钮不可点击
     $("button").style.backgroundColor = "#FFC09F";
    }
   }
   //按钮事件
   $("button").onmousemove = function () {
    //当文本域不为空执行下面按钮事件
    if($("text").value != ""){
     $("button").onmouseout= function () {
      //如果不为空 按钮为亮色
      if($("text").value != ""){
this.style.backgroundColor = "#FF8140";
      }else {
       //恢复按钮为不可点击颜色
       $("button").style.backgroundColor = "#FFC09F";
      }
     }
     //按钮为高亮
     this.style.backgroundColor = "#F7671D";
     //调用节点函数,并把传递当前文本内容
     $("button").onclick = function () {
      if($("text").value != ""){
       nweChild("order",$("text").value);
      }
      //清空当前文本
      $("text").value = "";
      //恢复按钮为不可点击颜色
$("button").style.backgroundColor = "#FFC09F";
      //更改提示文本内容
      $("txt").innerHTML = "你看,没骗你吧!"
      //显示提示文本
      $("txt").style.display = "block";
     }
    }
   }
  }
 </script>
</head>
<body>
 <div class="box">
  <span class="H">有什么新鲜事想告诉大家?</span>
  <textarea name="text" id="text"></textarea>
  <label for="text" id="txt">输入一段话,点发布会有惊喜哦!</label>
  <span id="button">发布</span>
 </div>
 <div class="box dynamic" id="order">
  <span class="time">2016-12-07 21:20</span>
  <div class="user">
   <img src="http://cdn.attach.qdfuns.com/notes/pics/201612/07/212742m96ugh87fzxhuuxp.jpg" width=50; alt="头像">
   <span class="name">漫步未来</span>
  </div>
  <ul class="list">
   <li>未来的你,一定会感谢现在拼命的自己。</li>
  </ul>
 </div>
</body>
</html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

相关文章

  • js滚动条多种样式,推荐

    js滚动条多种样式,推荐

    [红色]js滚动条多种样式,推荐...
    2007-02-02
  • 浅谈利用JavaScript进行的DDoS攻击原理与防御

    浅谈利用JavaScript进行的DDoS攻击原理与防御

    这篇文章主要介绍了浅谈利用JavaScript进行的DDoS攻击原理与防御,以及介绍了相关的中间人攻击原理,需要的朋友可以参考下
    2015-06-06
  • 微信小程序实现鼠标拖动效果示例

    微信小程序实现鼠标拖动效果示例

    这篇文章主要介绍了微信小程序实现鼠标拖动效果,涉及微信小程序事件绑定及元素属性动态操作相关实现技巧,需要的朋友可以参考下
    2017-12-12
  • Bootstrap的图片轮播示例代码

    Bootstrap的图片轮播示例代码

    Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架。Bootstrap 是基于 HTML、CSS、JAVASCRIPT 的。本文给大家分享Bootstrap的图片轮播示例代码,小伙伴们快来围观吧。
    2015-08-08
  • JavaScript中的数组特性介绍

    JavaScript中的数组特性介绍

    这篇文章主要介绍了JavaScript中的数组特性介绍,本文总结了数组的3种特性和4种创建数组的方法,需要的朋友可以参考下
    2014-12-12
  • kindeditor编辑器点中图片滚动条往上顶的bug

    kindeditor编辑器点中图片滚动条往上顶的bug

    这篇文章主要介绍了kindeditor编辑器点中图片滚动条往上顶的bug的相关资料,需要的朋友可以参考下
    2015-07-07
  • javascript数组去重的六种方法汇总

    javascript数组去重的六种方法汇总

    数组去重复是一个常见的需求,我们暂时考虑同类型的数组去重复。主要是理清思路和考虑下性能。以下方法,有各人总结也有网上热心网友的方法,这里简单地总结一下。
    2015-08-08
  • Javascript 原型和继承(Prototypes and Inheritance)

    Javascript 原型和继承(Prototypes and Inheritance)

    前面我们看到了如何使用 constructor 来初始化对象。如果这样做,那么每一个创建的新对象都会对那些相同的属性,方法建立一个独立的副本。而实际上有更加有效的方法来指定方法,常量,以及其他一些可被所有该类的对象共享的属性。
    2009-04-04
  • js print打印网页指定区域内容的简单实例

    js print打印网页指定区域内容的简单实例

    下面小编就为大家带来一篇js print打印网页指定区域内容的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-11-11
  • bootstrap实现二级下拉菜单效果

    bootstrap实现二级下拉菜单效果

    这篇文章主要为大家详细介绍了bootstrap实现二级下拉菜单效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11

最新评论