Javascript 修复了Internet Explorer 11中无法使用的标头Java脚本
我使用的是一个固定头脚本,它在现代浏览器中运行良好,但在InternetExplorer11中根本不起作用。我可以看到它没有在IE中添加CSS所需的固定类。试图调试它,但控制台没有向我显示任何内容 Ì添加了document.querySelector polyfill,并开始使用window.pageYOffset代替原始代码。但在IE11中仍然不起作用 我想知道是否有可能是一个问题与投掷和火灾的事件在滚动(请看看脚本的结尾)Javascript 修复了Internet Explorer 11中无法使用的标头Java脚本,javascript,internet-explorer-11,Javascript,Internet Explorer 11,我使用的是一个固定头脚本,它在现代浏览器中运行良好,但在InternetExplorer11中根本不起作用。我可以看到它没有在IE中添加CSS所需的固定类。试图调试它,但控制台没有向我显示任何内容 Ì添加了document.querySelector polyfill,并开始使用window.pageYOffset代替原始代码。但在IE11中仍然不起作用 我想知道是否有可能是一个问题与投掷和火灾的事件在滚动(请看看脚本的结尾) /*修复了内容脚本2019-09-27 13.13*/ //修复旧浏
/*修复了内容脚本2019-09-27 13.13*/
//修复旧浏览器的document.querySelector
// https://gist.github.com/chrisjlee/8960575
如果(!document.querySelector){
document.querySelector=函数(选择器){
var元素=document.querySelectorAll(选择器);
返回(elements.length)?elements[0]:空;
};
}
//旧浏览器的结束修复
//为相关包装器创建变量
const stickyWrapper=document.querySelector(“.stickyWrapper”);
const navWrapper=document.querySelector(“.nav wrapper”);
const searchWrapper=document.querySelector(“.searchWrapper”);
const containerWrapper=document.querySelector(“.container wrapper”);
//获得高度##############
// ... 粘性包装纸
设stickyWrapperHeight=stickyWrapper.getBoundingClientRect().height;
// ... 导航包装器的设计
让navWrapperHeight=navWrapper.getBoundingClientRect().height;
// ... 搜索包装器的设计
让searchWrapperHeight=searchWrapper.getBoundingClientRect().height;
//获得最佳位置##############
// ... 导航包装器的设计
让navWrapperTop=navWrapper.getBoundingClientRect().top;
// ... 搜索包装器的设计
让searchWrapperTop=searchWrapper.getBoundingClientRect().top;
//获得原始位置##############
// ... 粘性包装纸
// 1. 取粘性包装的顶部位置
// 2. 去掉粘性包装纸的高度
// 3. 添加文档当前滚动的像素数
让stickyWrapperOrigPos=stickyWrapper.getBoundingClientRect().bottom-stickyWrapperHeight+window.pageYOffset;
// ... 搜索包装器的设计
// 1. 取粘性包装的底部位置
// 2. 去掉包装纸的高度
// 3. 添加文档当前滚动的像素数
让searchWrapperOrigPos=stickyWrapper.getBoundingClientRect().bottom-searchWrapperHeight+window.pageYOffset;
// ... 导航包装器的设计
// 1. 取粘性包装的底部位置
// 2. 去掉包装纸的高度
// 3. 去掉包装纸的高度
// 4. 添加文档当前滚动的像素数
让navWrapperOrigPos=stickyWrapper.getBoundingClientRect().top-searchWrapperHeight-navWrapperHeight+window.pageYOffset;
//获得最初的最高位置##############
// ... 粘性包装纸
// 1. 取粘性包装的顶部位置
让stickyWrapperOrigPosTop=stickyWrapper.getBoundingClientRect().top;
//获取原始底部位置##############
// ... 粘性包装纸
// 1. 取粘性包装的底部位置
让stickyWrapperOrigPosBot=stickyWrapper.getBoundingClientRect().bottom;
//在适当的时候启动和停止固定效果##############
函数stickyThingInit(){
//再次检查顶部位置(f.e.用户刷新页面)
navWrapperTop=navWrapper.getBoundingClientRect().top;
searchWrapperTop=searchWrapper.getBoundingClientRect().top;
searchWrapperHeight=searchWrapper.getBoundingClientRect().height;
//如果窗口的滚动Y位置在顶部位置
如果(window.pageYOffset>searchWrapperOrigPos){
//将固定类添加到stickyWrapper
stickyWrapper.classList.add(“stickyWrapper已修复”);
//将固定类添加到searchWrapper
searchWrapper.classList.add(“searchWrapper已修复”);
//将固定类添加到navWrapper
navWrapper.classList.add(“navWrapper已修复”);
//将searchWrapper的高度添加到导航wrapper中,使其显示在新的searchWrapper位置之后
navWrapper.style.top=searchWrapperHeight+“px”;
//将填充顶部添加到ContainerRapper以防止跳跃并使内容可见
ContainerRapper.style.paddingTop=(searchWrapperHeight+navWrapperHeight)+“px”;
}
//如果窗口的滚动Y位置和searchWrapper的高度以及navWrapper的高度小于stickyWrapper的顶部位置
如果(window.pageYOffset
jQuery(文档).ready(函数($){
$(“#巴”).mmenu({
“滑动子菜单”:false,
“扩展”:[
“fx菜单缩放”,
“fx面板缩放”,
“浅黑色”,
“黑暗主题”,
“边界偏移”
],
“导航栏”:[{
“位置”:“顶部”,
“内容”:[
"",
"",
"",
""
]
}, {
“位置”:“底部”,
“内容”:[
"",
"",
"",
""
]
}]
}, {
类名:{
固定元件:{
修正:“hdr修正器”
}
},
非画布:{
页面选择器:“#页面”
},
});
var API=$(“#bar”).数据(“mmenu”);
$(“#条”)。单击(函数(){
API.open();
});
});
.第页{
溢出:隐藏;
显示:博克
}
.酒吧{
显示:块
}
.标题{
溢出:隐藏;
宽度:100%;
最小高度:50px;
线高:50px;
填充:10px;
文本对齐:左对齐;
背景:#ffffff;
边框底部:1px#d9d9d9实心;
}
.hdr定影器{
位置:固定;
排名:0;
左:0;
z指数:7;
}
#条形图:非(.mm菜单){
显示:无;
}
.内容{
显示:块
}
-
/* FIXEDTHINGs-SCRIPT 2019-09-27 13.13 */
//Fix document.querySelector for old browser
// https://gist.github.com/chrisjlee/8960575
if (!document.querySelector) {
document.querySelector = function (selectors) {
var elements = document.querySelectorAll(selectors);
return (elements.length) ? elements[0] : null;
};
}
// End Fix for old browser
// ############## Create variables for relevant wrappers ##############
const stickyWrapper = document.querySelector(".sticky-wrapper");
const navWrapper = document.querySelector(".nav-wrapper");
const searchWrapper = document.querySelector(".search-wrapper");
const containerWrapper = document.querySelector(".container-wrapper");
// ############## GET HEIGHT ##############
// ... of stickyWrapper
let stickyWrapperHeight = stickyWrapper.getBoundingClientRect().height;
// ... of navWrapper
let navWrapperHeight = navWrapper.getBoundingClientRect().height;
// ... of searchWrapper
let searchWrapperHeight = searchWrapper.getBoundingClientRect().height;
// ############## GET TOP POSITION ##############
// ... of navWrapper
let navWrapperTop = navWrapper.getBoundingClientRect().top;
// ... of searchWrapper
let searchWrapperTop = searchWrapper.getBoundingClientRect().top;
// ############## GET ORIGINAL POSITION ##############
// ... of stickyWrapper
// 1. take top position of stickyWrapper
// 2. take away the height of stickyWrapper
// 3. add the number of pixels the doc has currently scrolled
let stickyWrapperOrigPos = stickyWrapper.getBoundingClientRect().bottom - stickyWrapperHeight + window.pageYOffset;
// ... of searchWrapper
// 1. take bottom position of StickyWrapper
// 2. take away the height of searchWrapper
// 3. add the number of pixels the doc has currently scrolled
let searchWrapperOrigPos = stickyWrapper.getBoundingClientRect().bottom - searchWrapperHeight + window.pageYOffset;
// ... of navWrapper
// 1. take bottom position of StickyWrapper
// 2. take away the height of searchWrapper
// 3. take away the height of navWrapper
// 4. add the number of pixels the doc has currently scrolled
let navWrapperOrigPos = stickyWrapper.getBoundingClientRect().top - searchWrapperHeight - navWrapperHeight + window.pageYOffset;
// ############## GET ORIGINAL TOP POSITION ##############
// ... of stickyWrapper
// 1. take top position of stickyWrapper
let stickyWrapperOrigPosTop = stickyWrapper.getBoundingClientRect().top;
// ############## GET ORIGINAL BOTTOM POSITION ##############
// ... of stickyWrapper
// 1. take bottom position of stickyWrapper
let stickyWrapperOrigPosBot = stickyWrapper.getBoundingClientRect().bottom;
// ############## START AND STOP FIXED-EFFECT AT THE RIGHT MOMENT ##############
function stickyThingInit() {
// check top-Position again (f.e. user refreshes page)
navWrapperTop = navWrapper.getBoundingClientRect().top;
searchWrapperTop = searchWrapper.getBoundingClientRect().top;
searchWrapperHeight = searchWrapper.getBoundingClientRect().height;
// if window's scroll Y position is at searchWrapper top position
if (window.pageYOffset > searchWrapperOrigPos) {
// add the fixed class to the stickyWrapper
stickyWrapper.classList.add("sticky-wrapper-fixed");
// add the fixed class to the searchWrapper
searchWrapper.classList.add("search-wrapper-fixed");
// add the fixed class to the navWrapper
navWrapper.classList.add("nav-wrapper-fixed");
// add the height of searchWrapper to the nav wrapper as top so that it shows after the new searchWrapper position
navWrapper.style.top = searchWrapperHeight + "px";
//add padding top to the containerWrapper to prevent jumping and make content visible
containerWrapper.style.paddingTop = (searchWrapperHeight + navWrapperHeight) + "px";
}
// If the window's scroll Y position and the height of the searchWrapper and the height of the navWrapper are less than the stickyWrapper top position
if (window.pageYOffset <= stickyWrapperOrigPosTop) {
// remove the sticky class from stickyWrapper
stickyWrapper.classList.remove("sticky-wrapper-fixed");
// remove the sticky class from searchWrapper
searchWrapper.classList.remove("search-wrapper-fixed");
// remove the sticky class from navWrapper
navWrapper.classList.remove("nav-wrapper-fixed");
// remove the height inline-style from navWrapper
navWrapper.removeAttribute("style");
// remove the height inline-style from containerWrapper
containerWrapper.removeAttribute("style");
}
}
// function that throttles the event listener (or any function we add)
function throttled(delay, fn) {
let lastCall = 0;
return function (...args) {
const now = (new Date).getTime();
if (now - lastCall < delay) {
return;
}
lastCall = now;
return fn(...args);
}
}
// On scroll, fire the function
throttled(400, window.addEventListener("scroll", stickyThingInit));
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
jQuery(document).ready(function($) {
$("#bar").mmenu({
"slidingSubmenus": false,
"extensions": [
"fx-menu-zoom",
"fx-panels-zoom",
"pagedim-black",
"theme-dark",
"border-offset"
],
"navbars": [{
"position": "top",
"content": [
"<a class='fa fa-user' target='_blank' href='https://facebook.com/mwemaco'></a>",
"<a class='fa fa-history' target='_blank' href='https://twitter.com/mwemaco'></a>",
"<a class='fa fa-envelope' target='_blank' href='https://youtube.com/mwemaco'></a>",
"<a class='fa fa-envelope' target='_blank' href='https://youtube.com/mwemaco'></a>"
]
}, {
"position": "bottom",
"content": [
"<a class='fa fa-facebook' target='_blank' href='https://facebook.com/mwemaco'></a>",
"<a class='fa fa-twitter' target='_blank' href='https://twitter.com/mwemaco'></a>",
"<a class='fa fa-youtube' target='_blank' href='https://youtube.com/mwemaco'></a>",
"<a class='fa fa-instagram' target='_blank' href='https://instagram.com/mwemaco'></a>"
]
}]
}, {
classNames: {
fixedElements: {
fixed: "hdr-fixer"
}
},
offCanvas: {
pageSelector: "#page"
},
});
var API = $("#bar").data("mmenu");
$("#bar").click(function() {
API.open();
});
});
</script>
<style type="text/css">
.page {
overflow: hidden;
display: bock
}
.bar {
display: block
}
.header {
overflow: hidden;
width: 100%;
min-height: 50px;
line-height: 50px;
padding: 10px;
text-align: left;
background: #ffffff;
border-bottom: 1px #d9d9d9 solid;
}
.hdr-fixer {
position: fixed;
top: 0;
left: 0;
z-index: 7;
}
#bar:not(.mm-menu) {
display: none;
}
.content {
display: block
}
</style>
</head>
<body>
<div id="page" class="page">
<div class="header hdr-fixer">
<a href="#bar">MENU</a>
</div>
<div id="bar" class="bar">
<ul>
<li><a href="#">Account</a></li>
<li><a href="#">App</a></li>
<li><a href="#">About</a>
<ul>
<li><a href="/about/history">History</a></li>
<li><a href="/about/team">The team</a></li>
<li><a href="/about/address">Our address</a></li>
</ul>
</li>
<li><a href="#">Support</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
<div class="content">
Hello
<br>
<br>Hello
<br>
<br>Hello
<br>
<br>Hello
<br>
<br>Hello
<br>
<br>Hello
<br>
<br>Hello
<br>
<br>Hello
<br>
<br>Hello
<br>
<br>Hello
<br>
<br>Hello
<br>
<br>Hello
<br>
<br>Hello
<br>
<br>Hello
<br>
<br>Hello
<br>
<br>Hello
<br>
<br>Hello
<br>
<br>
</div>
</div>
</body>
<html>