-
Notifications
You must be signed in to change notification settings - Fork 1
/
4_3_sticky_top_jquery_add_remove_class.html
90 lines (74 loc) · 2.51 KB
/
4_3_sticky_top_jquery_add_remove_class.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
n<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<style>
body {
margin: 0;
padding: 0;
}
.jumbotron {
margin-bottom: 0px;
}
#tofix1 {
z-index: 99;
}
#tofix2 {
z-index: 98;
}
.fixed {
position: fixed;
top: 0;
}
body {
color: #fff;
}
</style>
</head>
<body>
<div class="container-fluid" style="background-color:dodgerblue;width:100%;min-height:200px;">
<h3>affix by jquery: min-hight: 200px</h3>
</div>
<div id="tofix1" class="container-fluid" style="background-color:coral;width:100%;min-height:50px;">
<h3>tofix1: min-height: 50px</h3>
</div>
<div class="container-fluid" style="min-height:500px;background-color:lightgray;">
<h3>space</h3>
</div>
<div class="container-fluid" style="min-height:1000px;">
<div class="row">
<div class="col-md-4">
<svg id="tofix2" width="250" height="250">
<circle cx="100" cy="100" r="100" fill="coral" />
</svg>
</div>
<div class="col-md-8"></div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<!-- <script src="js/jquery-3.2.1.slim.min.js"></script>-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>
<script>
$(document).ready(function() {
var tofix1_height = $('#tofix1').offset().top; //Reserve the original height
var tofix2_height = $('#tofix2').offset().top; //Reserve the original height
$(window).scroll(function() {
var scrolled = $(window).scrollTop();
console.log(scrolled);
if (scrolled >= tofix1_height) {
$('#tofix1').addClass('fixed');
} else {
$('#tofix1').removeClass('fixed');
}
if (scrolled >= tofix2_height - 100) {
$('#tofix2').addClass('fixed');
} else {
$('#tofix2').removeClass('fixed');
}
});
});
</script>
</body>
</html>