-
Notifications
You must be signed in to change notification settings - Fork 0
/
ufclas-sticky-function.php
68 lines (56 loc) · 1.77 KB
/
ufclas-sticky-function.php
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
<?php
/*
* Plugin Name: UFCLAS Sticky Function
* Plugin URI: https://github.com/ufclas/ufclas-sticky-function/blob/main/README.md
* Description: This plugin is for making navigation stick using sticky style.
* Version: 1.0
* Author: Fnu Ojasvi
* Author URI: https://commsupport.clas.ufl.edu
*/
/*==================================
Set height of parent of sticky
====================================*/
function set_sticky_height() {
?>
<script>
if (document.querySelector('.sticky')) {
function setStickyHeight() {
const parentDiv = document.querySelector('.sibling-of-sticky');
const parent = document.querySelector('.parent-of-sticky');
const parentHeight = parentDiv.getBoundingClientRect().height;
parent.style.height = (parentHeight) + 'px';
// Add height style only if min-width is 768px
if (window.innerWidth >= 768) {
parent.style.height = (parentHeight) + 'px';
} else {
parent.style.height = 'auto';
}
}
setStickyHeight();
}
</script>
<?php
}
add_action('wp_footer', 'set_sticky_height');
/*==================================
Add sticky style
====================================*/
function add_sticky_style() {
?>
<style>
@media (min-width: 768px) {
.sticky {
position: -webkit-sticky;
position: sticky;
top: 0;
}
}
@media (max-width: 768px) {
.sticky {
display: none;
}
}
</style>
<?php
}
add_action('wp_head', 'add_sticky_style');