PageSourceSearch

https://www.xjtlu.edu.cn/wp-content/themes/xjtlu/assets/js/backtotop.js?ver=1.7

js xjtlu.edu.cn collected 2026-09-24 08:46:43 UTC 6,045 bytes, 207 lines download raw bytes

1// Utility function
2function Util () {};
3
4/*
5	class manipulation functions
6*/
7Util.hasClass = function(el, className) {
8	if (el.classList) return el.classList.contains(className);
9	else return !!el.className.match(new RegExp('(\\s|^)' + className + '(\\s|$)'));
10};
11
12Util.addClass = function(el, className) {
13	var classList = className.split(' ');
14 	if (el.classList) el.classList.add(classList[0]);
15 	else if (!Util.hasClass(el, classList[0])) el.className += " " + classList[0];
16 	if (classList.length > 1) Util.addClass(el, classList.slice(1).join(' '));
17};
18
19Util.removeClass = function(el, className) {
20	var classList = className.split(' ');
21	if (el.classList) el.classList.remove(classList[0]);
22	else if(Util.hasClass(el, classList[0])) {
23		var reg = new RegExp('(\\s|^)' + classList[0] + '(\\s|$)');
24		el.className=el.className.replace(reg, ' ');
25	}
26	if (classList.length > 1) Util.removeClass(el, classList.slice(1).join(' '));
27};
28
29Util.toggleClass = function(el, className, bool) {
30	if(bool) Util.addClass(el, className);
31	else Util.removeClass(el, className);
32};
33
34Util.setAttributes = function(el, attrs) {
35  for(var key in attrs) {
36    el.setAttribute(key, attrs[key]);
37  }
38};
39
40/*
41  DOM manipulation
42*/
43Util.getChildrenByClassName = function(el, className) {
44  var children = el.children,
45    childrenByClass = [];
46  for (var i = 0; i < el.children.length; i++) {
47    if (Util.hasClass(el.children[i], className)) childrenByClass.push(el.children[i]);
48  }
49  return childrenByClass;
50};
51
52/*
53	Animate height of an element
54*/
55Util.setHeight = function(start, to, element, duration, cb) {
56	var change = to - start,
57	    currentTime = null;
58
59  var animateHeight = function(timestamp){
60    if (!currentTime) currentTime = timestamp;
61    var progress = timestamp - currentTime;
62    var val = parseInt((progress/duration)*change + start);
63    element.setAttribute("style", "height:"+val+"px;");
64    if(progress < duration) {
65        window.requestAnimationFrame(animateHeight);
66    } else {
67    	cb();
68    }
69  };
70
71  //set the height of the element before starting animation -> fix bug on Safari
72  element.setAttribute("style", "height:"+start+"px;");
73  window.requestAnimationFrame(animateHeight);
74};
75
76/*
77	Smooth Scroll
78*/
79
80Util.scrollTo = function(final, duration, cb) {
81  var start = window.scrollY || document.documentElement.scrollTop,
82      currentTime = null;
83
84  var animateScroll = function(timestamp){
85  	if (!currentTime) currentTime = timestamp;
86    var progress = timestamp - currentTime;
87    if(progress > duration) progress = duration;
88    var val = Math.easeInOutQuad(progress, start, final-start, duration);
89    window.scrollTo(0, val);
90    if(progress < duration) {
91        window.requestAnimationFrame(animateScroll);
92    } else {
93      cb && cb();
94    }
95  };
96
97  window.requestAnimationFrame(animateScroll);
98};
99
100/*
101  Focus utility classes
102*/
103
104//Move focus to an element
105Util.moveFocus = function (element) {
106  if( !element ) element = document.getElementsByTagName("body")[0];
107  element.focus();
108  if (document.activeElement !== element) {
109    element.setAttribute('tabindex','-1');
110    element.focus();
111  }
112};
113
114/*
115  Misc
116*/
117
118Util.getIndexInArray = function(array, el) {
119  return Array.prototype.indexOf.call(array, el);
120};
121
122Util.cssSupports = function(property, value) {
123  if('CSS' in window) {
124    return CSS.supports(property, value);
125  } else {
126    var jsProperty = property.replace(/-([a-z])/g, function (g) { return g[1].toUpperCase();});
127    return jsProperty in document.body.style;
128  }
129};
130
131/*
132	Polyfills
133*/
134//Closest() method
135if (!Element.prototype.matches) {
136	Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector;
137}
138
139if (!Element.prototype.closest) {
140	Element.prototype.closest = function(s) {
141		var el = this;
142		if (!document.documentElement.contains(el)) return null;
143		do {
144			if (el.matches(s)) return el;
145			el = el.parentElement || el.parentNode;
146		} while (el !== null && el.nodeType === 1);
147		return null;
148	};
149}
150
151//Custom Event() constructor
152if ( typeof window.CustomEvent !== "function" ) {
153
154  function CustomEvent ( event, params ) {
155    params = params || { bubbles: false, cancelable: false, detail: undefined };
156    var evt = document.createEvent( 'CustomEvent' );
157    evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
158    return evt;
159   }
160
161  CustomEvent.prototype = window.Event.prototype;
162
163  window.CustomEvent = CustomEvent;
164}
165
166/*
167	Animation curves
168*/
169Math.easeInOutQuad = function (t, b, c, d) {
170	t /= d/2;
171	if (t < 1) return c/2*t*t + b;
172	t--;
173	return -c/2 * (t*(t-2) - 1) + b;
174};
175
176
177(function(){
178  // Back to Top - by CodyHouse.co
179var backTop = document.getElementsByClassName('js-cd-top')[0],
180  offset = 1800, // browser window scroll (in pixels) after which the "back to top" link is shown
181  offsetOpacity = 1200, //browser window scroll (in pixels) after which the "back to top" link opacity is reduced
182  scrollDuration = 300,
183  scrolling = false;
184
185if( backTop ) {
186  //update back to top visibility on scrolling
187  window.addEventListener("scroll", function(event) {
188    if( !scrolling ) {
189      scrolling = true;
190      (!window.requestAnimationFrame) ? setTimeout(checkBackToTop, 250) : window.requestAnimationFrame(checkBackToTop);
191    }
192  });
193
194  //smooth scroll to top
195  backTop.addEventListener('click', function(event) {
196    event.preventDefault();
197    (!window.requestAnimationFrame) ? window.scrollTo(0, 0) : Util.scrollTo(0, scrollDuration);
198  });
199}
200
201function checkBackToTop() {
202  var windowTop = window.scrollY || document.documentElement.scrollTop;
203  ( windowTop > offset ) ? Util.addClass(backTop, 'cd-top--is-visible') : Util.removeClass(backTop, 'cd-top--is-visible cd-top--fade-out');
204  ( windowTop > offsetOpacity ) && Util.addClass(backTop, 'cd-top--fade-out');
205  scrolling = false;
206}
207})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.