用原生js写全屏滚动
2017-12-12 10:45
316 查看
css代码
<style>
body,div,ul,li{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
background-image: url(img/004.JPG) center cover;
}
#fullpage{
width:100%;
position: fixed;/*没有滚动条*/
top:50px;
transition:all 1s ease-in-out;
}
div{
width:100%;
}
.box1{
background: #f0c8ec;
}
.box2{
background: #db9def;
}
.box3{
background: #c2acf4;
}
.box4{
background: #acc9f4;
}
.box5{
background: #acf4f3;
}
.box6{
background: #acf4d0;
}
.box7{
background: #ecf4ac;
}
.nav{
width:100%;
height: 50px;
background: red;
position: fixed;
top:0;
z-index: 99;
}
.nav li{
width:120px;
height: 50px;
list-style: none;
font-size: 20px;
color: white;
font-weight: bold;
text-align: center;
line-height: 50px;
float: left;
}
</style>
html代码
<ul class="nav">
<li>nav1</li>
<li>nav2</li>
<li>nav3</li>
<li>nav4</li>
<li>nav5</li>
<li>nav6</li>
<li>nav7</li>
</ul>
<div id="fullpage">
<div class="box1">second1</div>
<div class="box2">second2</div>
<div class="box3">second3</div>
<div class="box4">second4</div>
<div class="box5">second5</div>
<div class="box6">second6</div>
<div class="box7">second7</div>
</div>
javascript代码
<script type="text/javascript">
var _fullpage = document.getElementById("fullpage");
var _div = _fullpage.getElementsByTagName("div");
var _lis = document.getElementsByTagName("li");
var h = (window.innerHeight || document.documentElement.clientHeight) - 50;
_lis[0].style.backgroundColor = "#000";
for(var i = 0;i < _div.length;i++){
_div[i].style.height = h + "px";
}
function addE(obj,type,fn){
if(obj.addEventListener){
obj.addEventListener(type,fn);
}else if(attachEvent){
obj.attachEvent("on" + type,fn);
}
}
addE(document,"mousewheel",wheel);
addE(document,"DOMMouseScroll",wheel);
//滚轮事件
function getWD(evt){
var e = evt || window.event;
if(e.wheelDelta){
return e.wheelDelta;
}else if(e.detail){
return -e.detail*40;
}
}
for(var k = 0;k < _lis.length;k++){
var a = _lis[k]; //获得索引号
a.index = k;
a.onmouseover =function(){
clic(this.index);
check(this.index);
}
}
function clic(n){
_fullpage.style.top = -h * n + 50 + "px";
}
function check(s){
for(var b = 0;b < _lis.length;b++){
if(b == s){
_lis[b].style.backgroundColor = "#000";
}else{
_lis[b].style.backgroundColor = "transparent";
}
}
}
var j=0;
function wheel(ev){
if(getWD(ev) < 0){
j++;
}
if(getWD(ev) > 0){
j--;
}
if(j >= 7){
j = 0;
}
if(j < 0){
j = 6;
}
_fullpage.style.top = -h * j + 50 + "px";
check(j);
}
</script>
<style>
body,div,ul,li{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
background-image: url(img/004.JPG) center cover;
}
#fullpage{
width:100%;
position: fixed;/*没有滚动条*/
top:50px;
transition:all 1s ease-in-out;
}
div{
width:100%;
}
.box1{
background: #f0c8ec;
}
.box2{
background: #db9def;
}
.box3{
background: #c2acf4;
}
.box4{
background: #acc9f4;
}
.box5{
background: #acf4f3;
}
.box6{
background: #acf4d0;
}
.box7{
background: #ecf4ac;
}
.nav{
width:100%;
height: 50px;
background: red;
position: fixed;
top:0;
z-index: 99;
}
.nav li{
width:120px;
height: 50px;
list-style: none;
font-size: 20px;
color: white;
font-weight: bold;
text-align: center;
line-height: 50px;
float: left;
}
</style>
html代码
<ul class="nav">
<li>nav1</li>
<li>nav2</li>
<li>nav3</li>
<li>nav4</li>
<li>nav5</li>
<li>nav6</li>
<li>nav7</li>
</ul>
<div id="fullpage">
<div class="box1">second1</div>
<div class="box2">second2</div>
<div class="box3">second3</div>
<div class="box4">second4</div>
<div class="box5">second5</div>
<div class="box6">second6</div>
<div class="box7">second7</div>
</div>
javascript代码
<script type="text/javascript">
var _fullpage = document.getElementById("fullpage");
var _div = _fullpage.getElementsByTagName("div");
var _lis = document.getElementsByTagName("li");
var h = (window.innerHeight || document.documentElement.clientHeight) - 50;
_lis[0].style.backgroundColor = "#000";
for(var i = 0;i < _div.length;i++){
_div[i].style.height = h + "px";
}
function addE(obj,type,fn){
if(obj.addEventListener){
obj.addEventListener(type,fn);
}else if(attachEvent){
obj.attachEvent("on" + type,fn);
}
}
addE(document,"mousewheel",wheel);
addE(document,"DOMMouseScroll",wheel);
//滚轮事件
function getWD(evt){
var e = evt || window.event;
if(e.wheelDelta){
return e.wheelDelta;
}else if(e.detail){
return -e.detail*40;
}
}
for(var k = 0;k < _lis.length;k++){
var a = _lis[k]; //获得索引号
a.index = k;
a.onmouseover =function(){
clic(this.index);
check(this.index);
}
}
function clic(n){
_fullpage.style.top = -h * n + 50 + "px";
}
function check(s){
for(var b = 0;b < _lis.length;b++){
if(b == s){
_lis[b].style.backgroundColor = "#000";
}else{
_lis[b].style.backgroundColor = "transparent";
}
}
}
var j=0;
function wheel(ev){
if(getWD(ev) < 0){
j++;
}
if(getWD(ev) > 0){
j--;
}
if(j >= 7){
j = 0;
}
if(j < 0){
j = 6;
}
_fullpage.style.top = -h * j + 50 + "px";
check(j);
}
</script>
相关文章推荐
- 原生js实现类似fullpage的单页/全屏滚动
- 原生 js 实现全屏滚动效果
- 原生Js页面滚动延迟加载图片
- (原生JS) 图片无缝滚动、图片转化、轮播图、折叠效果
- FullPage.js全屏滚动插件的配置项、方法和回调函数
- jQuery全屏滚动插件fullPage.js
- 使用jQuery或者原生js实现鼠标滚动加载页面新数据
- jQuery-全屏滚动插件【fullPage.js】API 使用方法总结
- jQuery全屏滚动插件fullPage.js
- jQuery全屏滚动插件FullPage.js
- 使用fullpage.js实现全屏滚动效果
- fullpage.js全屏滚动插件使用实例
- FullPage.js – 轻松实现全屏滚动(单页网站)效果
- fullpage.js---全屏滚动插件
- 原生JS绑定滑轮滚动事件兼容常见浏览器
- jQuery fullPage.js 全屏滚动
- 滚动页面时DIV到顶部时fixed(原生js实现)
- 利用FullPage.js实现全屏滚动插件
- 全屏滚动实现:fullPage.js和fullPage
- 原生JS:焦点图 - 左右滚动