js控住DOM实现发布微博效果
作者:骑猪敲代码 发布时间:2024-04-10 11:02:03
标签:js,微博
这段代码的效果具体是输入标题和内容,点击发布把消息发布出去,并使最新的消息始终在内容的最上面,代码为:
<!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节点操作的知识,纯属学习之余练手作品,大家可以参考参考。


猜你喜欢
- 首先需要安装itchat库,可以pip install itchat安装,也可以在pycharm里安装# -*- coding:utf-8
- 在MAC/LINUX环境下,执行vi hello.py命令,并输入以下代码import webimport sysurls = ("
- 可以让我们将数据用表格的方式展示出来安装方式pip install PrettyTable测试是否安装成功使用方法与对比增加一条数据先简单的
- 下策——查询出结果后将时间排序后取第一条select * from a where create_time<="2017-0
- mysql行转列、列转行 语句不难,不做多余解释了,看语句时,从内往外一句一句剖析行转列 &nb
- 标准的SQL模式匹配SQL的模式匹配允许你使用“_”匹配任何单个字符,而“%”匹配任意数目字符(包括零个字符)。在 MySQL中,SQL的模
- 本文实例为大家分享了wxPython绘图模块wxPyPlot实现数据可视化的具体代码,供大家参考,具体内容如下#-*- coding: ut
- 项目地址这个项目很神奇,直接将node.js项目打包成windows可以直接执行的exe文件(也支持FreeBSD、linux、macos、
- 下表列出了所有Python语言支持的赋值运算符。假设变量a持有10和变量b持有20,则: 例如:试试下面的例子就明白了所有在Pyt
- 一、基本概念Reactive X中有几个核心的概念,先来简单介绍一下。1.1、Observable和Observer(可观察对象和观察者)首
- 今天,老师带偶们复习了一下数据库中的分页,总体来说,今天感觉还不错,因为以前学的还没忘。好了,进入正题,首先,说说top的方法top方法其实
- 症状: 访问asp网页的时候出现如下错误:Server object error ''ASP 0178 : 80070005
- 本文实例为大家分享了python3 pygame接小球游戏的具体代码,供大家参考,具体内容如下操作方法:鼠标操作截图:直接放代码:# -*-
- 目录Ⅰ. 简介Ⅱ. 注意事项Ⅲ. 使用方法Ⅳ. 教程首先spring自带了mongodb的orm,spring data mongodb,但
- 在网上找了找也没找到合适的最后自己测试用 存入:Replace("\r\n", "<br/>&qu
- 本文实例讲述了python操作redis的方法。分享给大家供大家参考。具体如下:#!/usr/bin/python#coding=utf-8
- 这个操作现在看来真没啥难的,但是我找相关的资料真的找了好久。多数大佬都是直接pandas官网甩我脸上,然后举一个入门级的例子。https:/
- 前言本文给大家详细介绍了解决php-fpm.service not found问题的相关内容,文中介绍的非常详细,下面来一起看看详细的介绍:
- numpy中轴参数的意义指定的轴是被压缩的轴沿轴的时候可以指定两个轴,即面被压缩,以面作为输入numpy中轴转动numpy中添加新轴np.n
- Python2>>> >>> isinstance(b'abc', bytes)True