such as the title, I want to make a when scrolling to the head of the currently selected element is fixed, and then down to the original position, the current display
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.js"></script>
<style>
html,
body {
padding: 0;
}
-sharpheader {
line-height: 70px;
text-align: center;
background-color: red;
}
-sharpnav {
height: 30px;
margin-top: 20px;
background-color: blue;
}
-sharpwrap {
height: 1200px;
background-color: -sharpccc;
padding: 10px;
padding-right: 20px;
}
-sharpwrap .aside {
float: left;
width: 100px;
height: 400px;
background-color: green;
float: left;
}
-sharpwrap .main {
background-color: white;
overflow: hidden;
height: 100%;
}
-sharpfooter {
height: 30px;
background-color: black;
color: green;
}
</style>
</head>
<body>
<div id="header">header</div>
<div id="nav">nav</div>
<div id="wrap">
<div class="aside">aside</div>
<div class="main">main</div>
</div>
<div id="footer">footer</div>
<script>
var this1;
$.fn.stick = function () {
this1 = this;
$(window).on("scroll", function () {
var wintop = $(window).scrollTop();
if(wintop>=objtop){
console.log(1);
if(!this1.attr("inx")){
this1.hide();
cloneself.css({
"display": "block",
"opacity": "1",
"position":"fixed",
"top":0,
"margin":0
})
}
this1.attr("inx","true");
return;
}else{
if(!this1.attr("inx")){
return;
}else{
console.log(2);
cloneself.hide();
this1.show();
this1.removeAttr("inx");
return;
}
}
})
}
$("-sharpnav").stick();
var objtop = $(this1).offset().top;
var objwidth = $(this1).width();
var objheight = $(this1).height();
var cloneself = $(this1).clone();
cloneself.css({
"display": "none",
"opacity": "0",
"width": objwidth,
"height": objheight
})
cloneself.insertBefore(this1);
</script>
</body>
</html>
does not the bug, that the if else always executes at the same time be executed only once by the if else? Why does this bug?
appear?if I changed the code like this, I wouldn"t have this problem. What"s going on?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.js"></script>
<style>
html,
body {
padding: 0;
}
-sharpheader {
line-height: 70px;
text-align: center;
background-color: red;
}
-sharpnav {
height: 30px;
margin-top: 20px;
background-color: blue;
}
-sharpwrap {
height: 1200px;
background-color: -sharpccc;
padding: 10px;
padding-right: 20px;
}
-sharpwrap .aside {
float: left;
width: 100px;
height: 400px;
background-color: green;
float: left;
}
-sharpwrap .main {
background-color: white;
overflow: hidden;
height: 100%;
}
-sharpfooter {
height: 30px;
background-color: black;
color: green;
}
</style>
</head>
<body>
<div id="header">header</div>
<div id="nav">nav</div>
<div id="wrap">
<div class="aside">aside</div>
<div class="main">main</div>
</div>
<div id="footer">footer</div>
<script>
var this1;
$.fn.stick = function () {
this1 = this;
$(window).on("scroll", function () {
var wintop = $(window).scrollTop();
if(wintop>=objtop){
console.log(1);
}else{
console.log(2);
}
})
}
$("-sharpnav").stick();
var objtop = $(this1).offset().top;
var objwidth = $(this1).width();
var objheight = $(this1).height();
var cloneself = $(this1).clone();
cloneself.css({
"display": "none",
"opacity": "0",
"width": objwidth,
"height": objheight
})
cloneself.insertBefore(this1);
</script>
</body>
</html>
-it has been solved that when the scrolling distance is less than the top of the current element, I directly remove the style, and then restore the original value, which is fine
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.js"></script>
<style>
html,
body {
padding: 0;
}
-sharpheader {
line-height: 70px;
text-align: center;
background-color: red;
}
-sharpnav {
height: 30px;
margin-top: 20px;
background-color: blue;
}
-sharpwrap {
height: 1200px;
background-color: -sharpccc;
padding: 10px;
padding-right: 20px;
}
-sharpwrap .aside {
float: left;
width: 100px;
height: 400px;
background-color: green;
float: left;
}
-sharpwrap .main {
background-color: white;
overflow: hidden;
height: 100%;
}
-sharpfooter {
height: 30px;
background-color: black;
color: green;
}
</style>
</head>
<body>
<div id="header">header</div>
<div id="nav">nav</div>
<div id="wrap">
<div class="aside">aside</div>
<div class="main">main</div>
</div>
<div id="footer">footer</div>
<script>
var this1;
$.fn.stick = function () {
this1 = this;
$(window).on("scroll", function () {
var wintop = $(window).scrollTop();
if (wintop >= objtop) {
if (!this1.attr("inx")) {
this1.hide();
cloneself.css({
"display": "block",
"opacity": "1",
"position": "fixed",
"width": objwidth,
"height": objheight,
"top": 0,
"margin": 0
})
}
this1.attr("inx", "true");
return;
} else {
if (!this1.attr("inx")) {
return;
} else {
cloneself.removeAttr("style")
cloneself.css({
"display": "none",
"opacity": "0",
})
this1.show();
this1.removeAttr("inx");
return;
}
}
})
}
$("-sharpnav").stick();
var objtop = $(this1).offset().top;
var objleft = $(this1).offset().left;
var objwidth = $(this1).width();
var objheight = $(this1).height();
var cloneself = $(this1).clone();
cloneself.css({
"display": "none",
"opacity": "0",
})
cloneself.insertBefore(this1);
</script>
</body>
</html>