这段代码的效果具体是输入标题和内容,点击发布把消息发布出去,并使最新的消息始终在内容的最上面,代码为:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>微博消息效果</title>
<style>
*{margin:0;padding: 0;}
.warp{
width: 600px;
margin:50px auto 0;
background-color: #ccc;
}
#box{
width: 600px;
height: 340px;
/*background-color: #ccc;*/
position: relative;
/*overflow: hidden;*/
/*margin:50px auto 0;*/
font-family: '微软雅黑';
}
#box .span1{
position: absolute;
font-size: 16px;
line-height: 16px;
top: 10px;
left: 5px;
}
#box .span2{
position: absolute;
font-size: 16px;
line-height: 16px;
top: 50px;
left: 5px;
}
#title{
position: absolute;
width: 460px;
height: 20px;
line-height: 20px;
font-size: 16px;
text-indent: 5px;
left: 70px;
top: 6px;
}
#text{
position: absolute;
width: 460px;
height: 250px;
resize: none;
top: 50px;
left: 70px;
text-indent: 5px;
font-size: 16px;
}
#box #prompt{
position: absolute;
top: 312px;
left: 340px;
}
#prompt1{
position: absolute;
top: 312px;
left: 340px;
display: none;
}
#send{
position: absolute;
height: 25px;
width: 60px;
line-height: 20px;
font-size: 16px;
top: 310px;
right: 68px;
}
#news{
list-style: none;
width: 490px;
margin:10px auto 0px;
padding-bottom: 5px;
}
#news li{
width: 490px;
font-size: 14px;
overflow: hidden;
background-color: #fff;
margin-bottom: 5px;
position: relative;
}
#news li h1{
font-size: 16px;
line-height: 20px;
}
#news li p{
text-indent: 5px;
clear: left;
}
#news li span{
position: absolute;
top: 0px;
right: 0px;
cursor: pointer;
}
#news span:hover{
color: red;
}
</style>
</head>
<body>
<div class="warp">
<div id="box">
<span class='span1'>标题:</span>
<input id="title" type="text">
<span class="span2">内容:</span>
<textarea id="text"></textarea>
<em id="prompt">还可以输入<var id="textnum">200</var>字</em>
<em id="prompt1">你已超出<var id="textnum1"></var>字</em>
<button id="send">发送</button>
</div>
<ul id="news">
<li><h1></h1><span></span>
<p></p>
</li>
</ul>
</div>
<script>
var title=document.getElementById('title');
var text=document.getElementById('text');
var send=document.getElementById('send');
var ul=document.getElementById('news');
var lis=ul.getElementsByTagName('li');
var prompt=document.getElementById('prompt');
var prompt1=document.getElementById('prompt1');
var textnum=document.getElementById('textnum');
var textnum1=document.getElementById('textnum1');
var timer1=null,timer2=null;
send.onclick=function(){
if (text.value==''||title.value=='') {
alert('亲~标题或内容不能为空');return false;
}
lis[0].innerHTML='<h1>'+title.value+'</h1><span>×</span><p>'+text.value+'</p>';
lis[0].children[1].setAttribute('id','close');
var newLi=document.createElement('li');
ul.insertBefore(newLi,lis[0]);
maxheight=lis[1].clientHeight;
lis[1].style.height=0+'px';
var x=0;
var minstep=0;
var maxstep=20;
var change=maxheight/maxstep;
clearInterval(timer1);
timer1=setInterval(function(){
minstep++;
if (minstep>=maxstep) {
clearInterval(timer1);
}
x+=change;
lis[1].style.height=x+'px';
},10)
title.value='';
text.value='';
var close=document.getElementById('close');
for (var i = 0; i < lis.length; i++) {
close.onclick=function(){
var isme=this.parentNode;
var x=this.parentNode.clientHeight;
var minstep=0;
var maxstep=20;
var change=x/maxstep;
clearInterval(timer1);
timer1=setInterval(function(){
minstep++;
if (minstep>=maxstep) {
clearInterval(timer1);
ul.removeChild(isme);
}
x-=change;
isme.style.height=x+'px';
},10)
// ul.removeChild(lis[i]);//不可以,不知道绑定的是第几个。
}
}
}
text.onfocus=function(){
// console.log(prompt.children[0].innerHTML);//children是指带有标签的子节点;
timer2=setInterval(function(){
if(text.value.length<190){
var num=200-text.value.length;
textnum.style.color='black';
// prompt.style.color='black';
textnum.innerHTML=num;//
// prompt.innerHTML='还可以输入<var id="textnum">'+num+'</var>字</em>';
}
if (text.value.length>=190&&text.value.length<=200){
var num=200-text.value.length;
// prompt.style.color='black';
textnum.style.color='red';//为什么不变红呢?因为这他妹的也是一个未来事件!
// prompt.innerHTML='还可以输入<var id="textnum">'+num+'</var>字</em>';
textnum.innerHTML=num;
}
if (text.value.length>200){
var num=text.value.length-200;
// prompt.style.color='red';
prompt.style.display='none';
prompt1.style.display='block';
textnum1.style.color='red';
textnum1.innerHTML=num; }
// console.log(text.value.length);
},50) }
text.onblur=function(){
clearInterval(timer2);
}
</script>
</body>
</html>

这段代码主要运用了一些DOM节点操作的知识,纯属学习之余练手作品,大家可以参考参考。

js控住DOM实现发布微博简单效果的更多相关文章

  1. beego+vue.js分离开发,结合发布,简单部署

    大家知道,golang开发的东西部署简单是它很大的卖点,一般的应用,生成的可执行文件直接放服务器上运行即可,不需要任何环境.当然,大型的应用才需要比如mysql,nginx等. 但是当vue.js出现 ...

  2. 原生js实现的一个随机颜色的简单效果

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta na ...

  3. JS——BOM、DOM

    BOM.DOM BOM window对象 history对象 location对象 screen对象 DOM DOM对HTML元素访问操作 HTML DOM树 DOM 节点 DOM访问HTML元素 D ...

  4. 【原生js】js动态添加dom,如何绑定事件

    首先要明白浏览器在加载页面的时候是按顺序来加载的,这样以来就很清楚了,js动态添加dom以后,这些dom并没有绑定事件,这个时候最简单的一个办法就是:将绑定事件的方法封装到一个函数A中,在动态添加完d ...

  5. JS 仿腾讯发表微博的效果

    JS 仿腾讯发表微博的效果 最近2天研究了下 腾讯发表微博的效果 特此来分享下,效果如下: 在此分享前 来谈谈本人编写代码的习惯,很多人会问我既然用的是jquery框架 为什么写的组件不用Jquery ...

  6. js 字符串转dom 和dom 转字符串

    js 字符串转dom 和dom 转字符串 博客分类: JavaScript   前言: 在javascript里面动态创建标准dom对象一般使用: var obj = document.createE ...

  7. 发布一个简单的npm包

    本文简单地记录了发布一个简单npm包的过程,以便后续参考使用. 初始化npm init 通过npm init创建一个package.json文件 D:\robin\lib\weapp-utils> ...

  8. NPM酷库:jsdom,纯JS实现的DOM

    NPM酷库,每天两分钟,了解一个流行NPM库. 昨天认识了一个在Node.js环境下操作HTML的库 cheerio,cheerio实现了jQuery接口,用起来十分方便.为什么不直接用jQuery呢 ...

  9. JS获取各种宽度、高度的简单介绍:

    JS获取各种宽度.高度的简单介绍: scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离 scrollTop:设置或获 ...

随机推荐

  1. Java多线程系列--“JUC原子类”02之 AtomicLong原子类

    概要 AtomicInteger, AtomicLong和AtomicBoolean这3个基本类型的原子类的原理和用法相似.本章以AtomicLong对基本类型的原子类进行介绍.内容包括:Atomic ...

  2. Spark入门实战系列--7.Spark Streaming(上)--实时流计算Spark Streaming原理介绍

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .Spark Streaming简介 1.1 概述 Spark Streaming 是Spa ...

  3. Windows Azure HandBook (8) Azure性能测试(1)

    <Windows Azure Platform 系列文章目录> 我们在项目上线之前,常常需要对部署在微软云上的应用软件做压力测试. 一般的压力测试,常常在本地计算机安装压力测试软件 (比如 ...

  4. MVC导出数据到EXCEL新方法:将视图或分部视图转换为HTML后再直接返回FileResult

    导出EXCEL方法总结 MVC导出数据到EXCEL的方法有很多种,常见的是: 1.采用EXCEL COM组件来动态生成XLS文件并保存到服务器上,然后转到该文件存放路径即可: 优点:可设置丰富的EXC ...

  5. Moon.Orm 5.0(MQL版)分页功能的设计(求指教,邀请您的加入)

    一.分页的分类及分析 1)分页的前置条件: 查询的目标条件.第几页.总页数(本质上由查询条件决定).每页条数.请求地址.按照什么字段怎样排序 2)目标结果: 数据列表,List<T>返回 ...

  6. 书写高效的CSS

    一.使用高效是CSS ①:使用外联样式替代行间样式或内嵌样式. 不推荐使用内联样式:<style></style> 不推荐使用内嵌样式:<p style="&q ...

  7. CHARFORMAT2 structure

    CHARFORMAT2 structure 包含在丰富的编辑控件中的字符格式设置的信息.charformat2是微软丰富的编辑CHARFORMAT结构2扩展.微软丰富的编辑2允许您使用结构与em_ge ...

  8. jquery基本选择器(.class选择器)

    //通过class属性值匹配元素<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ht ...

  9. 在linux中连接wifi

    分为以下步骤:-----------(键入以下命令的时候注意大小写与空格.) 1. 进入终端treminal 获取管理员权限---------------------- su 命令(# su) 2. ...

  10. JAVA抽象方法,接口

    抽象方法(例如:画方法) 抽象方法必须用abstract void修饰 抽象方法没有方法体(方法体就是方法的实现) 抽象方法和空方法体的方法不是同意概念. a)  public abstract vo ...