关于浏览器兼容的问题以及字符串编码的一个坑,鼠标滚动事件mousewheel和DOMMouseScroll

话不多说,附上代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" type="text/css" href="css/sp_common.css"/>
<style type="text/css">
body{
background: gray;
overflow: hidden;
}
.wrap-ul li{
width: 100%;
height: 100%;
color: #fff;
font-size: 50px;
text-align: center;
}
.wrap-ul li:first-child{
background: green;
}
.wrap-ul li:nth-child(2){
background: greenyellow;
}
.wrap-ul li:nth-child(3){
background: cyan;
}
.wrap-ul li:last-child{
background: darkslategray;
}
.item-ul{
height: 200px;
width: 50px;
position: fixed;
top: 45%;
right: 5%;
/*background: red;*/
}
.item-ul li{
width: 100%;
height: 25%;
line-height: 40px;
text-align: center;
color: white;
font-size: 18px;
border: 1px solid black;
border-radius: 50%;
margin-top: 2px;
cursor: pointer;
}
.li-active{
background: red;
color: #000000;
}
</style>
</head>
<body>
<ul class="wrap-ul w h">
<li>one</li>
<li>two</li>
<li>three</li>
<li>four</li>
</ul>

<ul class="item-ul">
<li class="li-active">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<script src="js/jquery.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$('body,html').animate({scrollTop: 0}, 1000);
var index=0;
var scrollHeight=$(window).innerHeight();

$('.item-ul li').click(function(){
index=$(this).index();
console.log(index);
$(this).addClass('li-active').siblings().removeClass('li-active');
$('body,html').animate({scrollTop: scrollHeight*index}, 1000);
/*$('body').scrollTop(1000);*/
});
$(window).resize(function(){
var w=$(window).width();
var h=$(window).height();
$('.wrap-ul li').css({width:w,height:h});
$('body,html').scrollTop(h*index);
});

//以下两个兼容性代码可以做一些合并处理,我就没做了。

//ie(ev.wheelDelta)和chrome(ev.deltaY)滚动事件的兼容
document.body.addEventListener('mousewheel',function(ev){
console.log(ev);
if (ev.deltaY>0 || ev.wheelDelta<0) {
/*console.log('下滚');*/
if(!$(document.body).is(':animated'))moveNext();


} else{
/*console.log('上滚');*/
if(!$(document.body).is(':animated'))movePrev();

}
});


//firefox的滚动事件
document.body.addEventListener('DOMMouseScroll',function(ev){
if (ev.detail>0) {
console.log(3)
if(!$(document.body).is(':animated'))moveNext();
} else{
if(!$(document.body).is(':animated'))movePrev();
}

});

function moveNext(){
/* if(index==0){*/
if(index<3){
$('body,html').animate({scrollTop: scrollHeight*(++index)}, 1000);
}
$('.item-ul li:eq('+index+')').addClass('li-active').siblings().removeClass('li-active');

/* }*/
}

function movePrev(){
if(index>0){
$('body,html').animate({scrollTop: scrollHeight*(--index)}, 1000);
}
$('.item-ul li:eq('+index+')').addClass('li-active').siblings().removeClass('li-active');
}
</script>

</body>
</html>


其中的sp_common.css文件内容为:

/***********通用基本类1************/
/*格式化样式*/
html,body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,button,fieldset,input,textarea,p,blockquote,table,th,td {margin:0;padding:0;}
table {border-collapse:collapse;border-spacing:0;}
header, footer, section, article, aside, nav, address, figure, figcaption, menu, details {display: block;}
fieldset,img {border:0}
address,caption,cite,code,dfn,em,strong,th,var {font-style:normal;font-weight:normal}
ol,ul {list-style:none}
a{text-decoration: none;}
a:hover{text-decoration:none;cursor: pointer;}
h1,h2,h3,h4,h5,h6 {font-size:100%;font-weight:normal}
html{
font-size: 10px;
}
/*.box-main{
overflow: hidden;
}*/
html,body{
height: 100%;
width: 100%;
font-weight: normal;
}
*{
box-sizing: border-box;
}
.w{width:100%}
.h{height:100%}
/*------------ text -------------*/
.tl { text-align:left}
.tc { text-align:center}
.tr { text-align:right}


/*------------ float ------------*/
.fl{float:left;display:inline-block}
.fr{float:right;display:inline-block}


/*------------ clear-float ---------*/
/*.clear{content: ".";display: block;clear: both;}*/
.clear{*zoom:1;}
.clear::after {
  content: "";
  display: table;
  clear: both;
}
.cursor{cursor: pointer;}
.border{border: 1px solid #CCCCCC}
.b-bottom{border-bottom: 1px solid #CCCCCC}
.b-top{border-top: 1px solid #CCCCCC}
.b-right{border-right: 1px solid #CCCCCC}
.b-left{border-left: 1px solid #CCCCCC}
.b-radius{border-radius: 50%}


/*------------ position ------------*/
.pr { position:relative;}
.pa { position:absolute;}
.pf { position: fixed;}


一开始我的html文件是这样命名的:“官网.html” 没错用了汉子。谷歌和火狐都能识别。ie识别不了,把“官网.html”改成 “dddd.html” 只要是英文,ie就打开了。原因就是你既然内容用的是utf-8,那么文件名也要符合utf-8的规范,,,本来想做一个滚轮事件的demo制作,没想到踩这个坑了!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三木森森の

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值