JavaWeb开发之模仿知乎首页完整代码

闲来无事,就根据知乎的首页,参考了一些资料模拟写了下,包含了动态的背景,以及登录和注册功能

登录这里使用的是spring security

注册是ajax发送的

具体代码很简单,一看就知道,包含了数据的检查等

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="jquery/jquery.js"></script>
<style>
body{padding: 0;margin: 0;background: #F7FAFC;}
a{text-decoration: none;}
.index-box{width:300px;height: auto;margin:0 auto;margin-top: 40px;}
.logo{background:url(images/header/headerNew4.png) no-repeat;;width: 350px;height: 100px;margin:0 auto}
.title{font-size: 18px;text-align: center;color: #707171;font-weight: bold;margin: 30px auto;}
.index-body{text-align: center;}
.nav-sliders{position: relative;display: inline-block;margin-bottom: 20px;}
.nav-sliders>a{font-size: 20px;display: inline-block;width:60px ;font-family: "微软雅黑";color: #999;cursor: pointer;float: left;$width}
.nav-sliders>a.active{color: #0f88eb;}
.nav-sliders>span{position: absolute;height: 2px;background:#0f88eb;display:block;left: 5px;width: 50px;bottom:-8px;}
.stage{position:absolute;left:0;top:0;width:100%;height:100%;background:#F7FAFC;}
.login-box{width: 300px;}
.wrap{border:1px solid #d5d5d5;border-radius: 5px;background: #fff;}
.wrap>div{position: relative;overflow: hidden;}
.wrap>div>input{width: 95%;border:none;padding:17px 2.5%;border-radius: 5px;}
.wrap>div>label.error{position: absolute;color: #c33;top: 0;line-height: 50px;transform: translate(25px,0);transition: all 0.5s ease-out;-webkit-transform: translate(25px,0);-moz-transform: translate(25px,0);opacity: 0;visibility:hidden;cursor: text;}
.wrap>div>label.move{transform: translate(0,0);transition: all 0.5s ease-out;-webkit-transform: translate(0,0);-moz-transform: translate(0,0);opacity: 1;visibility: visible;}
.password{border-top: solid 1px #d5d5d5 ;}
.code{right:115px ;}
.name{right:5px ;}
.loginName{right:5px ;}
.loginPass{right:5px ;}
.pass{right:5px ;}
.passagain{right:5px ;}
.button{height: 40px;background:#0f88eb;text-align: center;line-height: 40px;border-radius: 5px;margin-top: 25px;color: #fff;font-family: "微软雅黑";cursor: pointer;}
.remember{text-align: left;margin-top: 20px;font-family: "微软雅黑";color: #666666;}
.remember>a{float:right;cursor: pointer;color: #666666;}
.other{text-align: left;margin-top: 20px;font-family: "微软雅黑";color: #666666;overflow: hidden;}
.other>span{font-size: 14px;float: left;margin-top: 2px;cursor: pointer;}
.other>div{float: left; transition: all 1s ease-in;-webkit-transition: all 0.3s ease-in;opacity: 0;transform: translateX(-20px);-webkit-transform: translateX(-18px);-moz-transform: translateX(-18px);visibility: hidden;}
.other>div>a{margin-left: 20px;color: #666666;font-size: 15px;}
.other>.hidden{ transition: all 1s ease-in;display: block;-webkit-transition: all 0.3s ease-in;opacity: 1;transform: translateX(0);-webkit-transform: translateX(0);-moz-transform: translateX(0);visibility: visible;}
.download{border:solid 1px #0f88eb;height: 40px;line-height: 40px;border-radius: 5px;color:#0f88eb ;font-family: "微软雅黑";margin-top: 50px;cursor: pointer;position: relative;}
.download>.close{display: none;}
.download .pic{display:none;position: absolute;background: #fff;bottom: 78px;width: 310px;left: -10px;z-index: 5;padding: 40px 0;border-radius: 8px;box-shadow: 0 0 8px 0 rgba(0,0,0,.15);}
.registered-box{width: 300px; display: none;}
.text{font-size: 14px;margin-top: 20px;font-family: "微软雅黑";color: #666666;}
.text>a{color: #0f88eb;}
.verification-code{position: absolute;right:22px;top: 14px;font-family: "微软雅黑";font-size: 18px;cursor: pointer;}
#register:hover{opacity: 0.7;}
#login:hover{opacity: 0.7;}
</style>
</head>
<body>
<div class="index-box" style="z-index:9999;position:relative">
<div class="index-header">
<h1 class="logo"></h1>
<h2 class="title">Speed4C开发平台</h2>
</div>
<div class="index-body">
<div class="nav-sliders">
<a id="loginButton" class="login active">登录</a>
<a class="registered">注册</a>
<span class="on"></span>
</div>
<div class="change">
<!--登录开始-->
<div class="login-box">
<form id="loginForm" action="${pageContext.request.contextPath}/j_spring_security_check" method="post">
<div class="wrap">
<div class="phone">
<input type="email" id="username" name='j_username' value="" placeholder="用户名">
<label class="error loginName">请输入登录名(email地址)</label>
</div>
<div class="password">
<input type="password" id="password" name='j_password' placeholder="密码">
<label class="error loginPass">密码</label>
</div>
</div>
<div class="button" id="login">登录</div>
<div class="remember">
<label><input id="rememberMe" name="_spring_security_remember_me" type="checkbox">记住我:</label>
<a>无法登陆?</a>
</div>
</form>
<div class="other">
<span>社交账号登录</span>
<div class="other-login">
<a href ="javascript:volid(0);">扣扣</a>
<a href ="javascript:volid(0);">微信</a>
<a href ="javascript:volid(0);">微博</a>
</div>
</div>
<div class="other">
<span style="color:red">${sessionScope.SPRING_SECURITY_LAST_EXCEPTION.message}</span>
</div>
</div>
<!--登录结束-->
<!--注册开始-->
<div class="registered-box">
<form id="registerForm" method="post" action="registerUser.do">
<div class="wrap">
<div class="phone">
<input type="email" name="name" value="" id="name" placeholder="用户名(邮箱)">
<label class="error name">请输入用户名</label>
</div>
<!-- <div class="password">
<input type="text" id="phone" value="" placeholder="手机号">
<label class="error phone restet">请输入手机号</label>
</div> -->
<div class="password">
<input type="password" id="passwor" name="passwor" value="" placeholder="密码">
<label class="error pass" >请输入密码</label>
</div>
<div class="password">
<input type="password" id="pssworAgain" name="" value="" placeholder="再次输入密码">
<label class="error passagain">请再次输入密码</label>
</div>
<div class="password">
<input type="text" id="code" name="" maxlength="6" value="" placeholder="验证码">
<label class="error passwor code">请输入验证码</label>
<div class="verification-code" id="createCade"></div>
</div>
</div>
<div class="button" id="register">注册</div>
</form>
</div>
<!--注册结束-->
</div>
</div>
</div>
<canvas id="canvas" class="stage" style="z-index:1000"></canvas>
<script>
"use strict";(function(h,d){var g=typeof d==="string"?document.querySelector(d):d,f=g.getBoundingClientRect(),c=f.width,l=f.height,n=g.getContext("2d"),j={x:c/2,y:l/2,radius:180},k=40,e=60,a=[],b;g.width=c;g.height=l;h.raf=h.requestAnimationFrame||webkitRequestAnimationFrame||function(p){return setTimeout(p,1000/60);};h.caf=h.cancelAnimationFrame||webkitCancelAnimationFrame||function(p){clearTimeout(p);};Function.prototype.method=function(q,p){return this.prototype[q]=p,this;};function i(p,r,q){this.x=this.ox=p;this.y=this.oy=r;this.radius=Math.random()*1+2;this.timer=q|0;}i.method("draw",function(p,v,r){var s=this.closest,q,u,t=this.getAlpha(r);if(t>0){p.fillStyle=p.strokeStyle="rgba(156,217,249,"+t+")";p.beginPath();p.arc(this.x,this.y,this.radius,0,Math.PI*2,true);p.closePath();p.fill();if(s){q=s.length;while(q--){u=v[s[q]];p.beginPath();p.moveTo(this.x,this.y);p.lineTo(u.x,u.y);p.stroke();}}}if(this._isMove){this.move();return;}if(this.timer++===this._moveFrames){this.setMove();}}).method("setMove",function(){this._isMove=true;this._frames=Math.random()*100+120;this._frame=0;this._tx=this.ox+Math.random()*100-50;this._ty=this.oy+Math.random()*100-50;}).method("move",function(){this.x=this.ease(this._frame++,this.x,this._tx-this.x,this._frames);this.y=this.ease(this._frame,this.y,this._ty-this.y,this._frames);if(Math.abs(this.x-this._tx)<0.5&&Math.abs(this.y-this._ty)<0.5){this._isMove=false;this.timer=0;}}).method("getAlpha",function(s){var q=this.x-s.x,p=this.y-s.y,u=Math.sqrt(q*q+p*p),t=s.radius;return u>t?0:(1-u/t)*0.6;}).method("ease",function(q,p,s,r){if((q/=r/2)<1){return s/2*q*q+p;}return -s/2*((--q)*(q-2)-1)+p;}).method("_moveFrames",e);function m(){var q=Math.max(60,c*1.5/k),t=l*1.5/q+0.5|0,v,r=0,s,u,p;v=c/q+0.5|0;for(;r<t;r++){for(s=0;s<v;s++){u=new i(s*q+(Math.random()*q*2-q),r*q+(Math.random()*q*2-q),Math.random()*e);p=r*v+s;a[p]=u;if(r&1&&s&&1){u.closest=[p-1,p-v,p-v-1];s<v-1&&u.closest.push(p+1);r<t-1&&u.closest.push(p+v);}}}o();}function o(){n.clearRect(0,0,c,l);a.forEach(function(r,q,p){r.draw(n,p,j);});b=raf(o);}g.addEventListener("mousemove",function(p){j.x=p.clientX-f.left;j.y=p.clientY-f.top;},false);h.addEventListener("resize",function(){caf(b);a=[];f=g.getBoundingClientRect();g.width=c=f.width;g.height=l=f.height;m();},false);m();})(this,document.querySelector(".stage"));
</script>
<script>
var code;//定义一个全局验证码
$(function(){
jcPublic.register();
jcPublic.Tab();
jcPublic.login();
/* jcPublic.downLoad(); */
jcPublic.createCode();
jcPublic.clickCode();
$(".wrap>div>input").focus(function(){
$(this).css({"outline": "none" });
var $this = $(this);
$this.next("label").removeClass("move");//隐藏提示信息
});
$(".other>span").on("click",function(){
$(this).parent().children("div").toggleClass("hidden");
});
});
var jcPublic = {
register:function(){//注册
var currentThis = this;//当前对象
$("#register").on("click",function(){
var $this = $(this);
var name = $("#name").val();//姓名
var registerName = document.getElementById("name");
var passwor = $("#passwor").val();
var passworAgain = $("#pssworAgain").val();
var Code = $("#code").val();//验证码
if(name.length==0 && passwor.length==0){//同时为空
$this.prev(".wrap").find("label").addClass("move");
return false;
}else if(name == "" || name == "undefined" || name == "null"){
$this.prev(".wrap").find(".name").html("请输入用户名").addClass("move");
return false;
}else if(!registerName.checkValidity()){
$this.prev(".wrap").find(".name").html("请输入有效的email地址").addClass("move");
return false;
}else if(passwor == "" || passwor == "undefined" || passwor == null ){
$this.prev(".wrap").find(".pass").addClass("move");
return false;
}else if(passworAgain == "" || passworAgain == "undefined" || passworAgain == null ){
$this.prev(".wrap").find(".passagain").addClass("move");
return false;
}else if(passworAgain == "" || passworAgain == "undefined" || passworAgain == null ){
$this.prev(".wrap").find(".passagain").addClass("move");
return false;
}else if(passworAgain != passwor){
$this.prev(".wrap").find(".passagain").html("请确保输入的密码一致").addClass("move");
return false;
}else if(code !== Code ){
$this.prev(".wrap").find(".code").html("验证码有误").addClass("move");
return currentThis.createCode();//验证码输入有误就刷新验证码重新输入
}else{
//发送添加用户申请
$.ajax({
type : "POST",
url : "registerUser.do",
data : {"name":name,"password":passwor},
dataType: "json",
error: function(XMLHttpRequest, textStatus, errorThrown){
alert("Error:"+XMLHttpRequest.status+",,,"+textStatus);
},
success : function(msg) {
alert(msg.messageString);
if(msg.messageString == "注册成功"){
$("#loginButton").click();
$("#registerForm")[0].reset();
}
}
});
/* $("#registerForm").ajaxSubmit(function(message){
alert(message.messageString);
}); */
}
});
},
Tab:function(){//切换注册和登录
$(".nav-sliders>a").on("click",function(){
$(this).addClass("active").siblings().removeClass("active");
var $width = $(this).width();//当前a的宽度
var $index = $(this).index();//索引
$(".on").stop(true,true).animate({"left":$width*$index+5+"px"},300);
$(this).parents(".index-body").children(".change").children().eq($index).stop(true,false).show().siblings().hide();
});
},
login:function(){
$("#login").on("click",function(){
var $this = $(this);
var username = $("#username").val();
var password = $("#password").val();
var checkloginName = document.getElementById("username");
if((password.length==0) && (username.length==0)){//同时为空
$this.prev(".wrap").find("label").addClass("move");
return false;
}else if(username == "" || username == "undefined" || username == null ){
$this.prev(".wrap").find(".loginName").addClass("move");
return false;
}else if(!checkloginName.checkValidity()){
$this.prev(".wrap").find(".loginName").html("请输入有效的email地址").addClass("move");
return false;
}else if(password == "" || password == "undefined" || password == null ){
$this.prev(".wrap").find(".loginPass").addClass("move");
return false;
}else{
//全部通过再提交
$("#loginForm").submit();
}
});
},
downLoad:function(){
$(".download").on("click",function(){
$(this).children(".pic").toggle(300);
});
},
createCode:function(){//验证码
var selectChar = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');//所有候选组成验证码的字符,也可以用中文的
code="";
var codeLength=4;//验证码的长度
for(var i =0;i<codeLength;i++){
var index = Math.floor(Math.random()*selectChar.length);//随机数
code +=selectChar[index];
//$("#createCade").html(code)
}
return $("#createCade").html(code);
},
clickCode:function(){//切换验证码
var $this = this;
$("#createCade").on("click",function(){
return $this.createCode();
});
}
};
</script>
</body>
</html>

以上所述是小编给大家介绍的JavaWeb开发之模仿知乎首页完整代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

(0)

相关推荐

  • JavaWeb实现同一帐号同一时间只能一个地点登陆(类似QQ登录的功能)

    JavaWeb实现同一帐号同一时间只能一个地点登陆(类似QQ登录的功能)的实现思路如下所示: 一.该功能有什么作用 大家想想吧.反正总会有这样的需求的.这年头什么需求不会有..呵呵.有时候也不一定是需求,很有可能为了安全也会这么做.例如考试系统,在线聊天系统,很有必要做成这样的吧. 二.实现过程 a.问题分析 在系统中,我们一般都是把登录信息绑定到session中,看来从这入手是可能找到解决办法.说白了,也就是当用户登录时,判断一下这个用户有没有登录,如果登录了,就把以前的那个session清除

  • javaweb 实现文件下载的方法及实例代码

    javaweb 实现文件下载 不要再说用<a>标签下载了,这个会把文件打开而不是下载 例如: <a href="E:\MyDesktop\37fecd65330184de67d419a8d02e7081.jpg">下载</a> 如果我这样写,浏览器就会把图片直接打开,除非是一个浏览器打不开的文件 所以我们还是要用到java本身的文件读写来进行文件的下载 <a href="downloadFile?filename=<s:prop

  • javaWeb使用Kaptcha组件生成验证码

    javaWeb之使用Kaptcha组件使用验证码 web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="htt

  • JavaWeb 简单分页实现代码

    JavaWeb 简单分页的实现: 这次主要是讲解一下通过登录后对得到的数据进行分页,首先我们新建一个登录页面login.jsp,因为我们主要学习一下分页,所以登录验证的部分不再阐述,主要代码如下: <form action="pageServlet"> 用户名:<input type="text" name="username"><br> 密 码:<input type="text"

  • JavaWeb实现文件上传下载功能实例详解

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 文件上传概述 1.文件上传的作用 例如网络硬盘!就是用来上传下载文件的. 在智联招聘上填写一个完整的简历还需要上传照片呢. 2.文件上传对页面的要求 上传文件的要求比较多,需要记一下: 必须使用表单,而不能是超链接 表单的method必须是POST,而不能是GET 表单的enctype必须是multipart/form-data 在表单中添加file表单字段,即<input ty

  • JavaWeb登陆功能实现代码

    本文实例为大家分享了JavaWeb登陆功能的方法,供大家参考,具体内容如下 首先我们要JavaWeb登陆的基本流程:JSP页面发送请求-->Servlet-->Servlet通过调用方法从数据库中得到数据并将结果返回页面. 我们先建立三个jsp页面,包括login.jsp(登陆页面).index.jsp(显示登陆成功后的信息).error.jsp(登录失败的页面),其中后两个页面的内容可以随意写,而login.jsp页面的主要内容如下: <form action="LoginS

  • JavaWeb利用struts实现文件下载时改变文件名称

    先看看效果图: package com.fenghuo.struts.download; import java.net.URLEncoder; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.

  • Javaweb实现上传下载文件的多种方法

    在Javaweb中,上传下载是经常用到的功能,对于文件上传,浏览器在上传的过程中是以流的过程将文件传给服务器,一般都是使用commons-fileupload这个包实现上传功能,因为commons-fileupload依赖于commons-io这个包,所以需要下载这两个包commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar. 1.搭建环境 创建Web项目,将包导入到项目lib下 2.实现文件上传 (第一种上传的方法) 新建upload.jsp页面 <

  • JavaWeb中的简单分页完整代码(推荐)

    这次主要是讲解一下通过登录后对得到的数据进行分页,首先我们新建一个登录页面login.jsp,因为我们主要学习一下分页,所以登录验证的部分不再阐述,主要代码如下: <form action="pageServlet"> 用户名:<input type="text" name="username"><br> 密 码:<input type="text" name="passwo

  • JavaWeb文件下载功能实例代码

    在工作中遇到的一个下载文件的功能,自己将其抽取出来,代码简单,希望能帮到大家,好了,话不多说,上代码! public void downloadFile(File file, String downName, HttpServletRequest request, HttpServletResponse response) { OutputStream out = null; FileInputStream fin = null; BufferedInputStream bin = null;

随机推荐