Difference between revisions of "Template:Pitt"
Line 7: | Line 7: | ||
<script> | <script> | ||
+ | |||
$(document).ready(function(){ | $(document).ready(function(){ | ||
+ | |||
+ | (function($) { | ||
+ | var defaults = { | ||
+ | topSpacing: 0, | ||
+ | bottomSpacing: 0, | ||
+ | className: 'is-sticky', | ||
+ | wrapperClassName: 'sticky-wrapper', | ||
+ | center: false, | ||
+ | getWidthFrom: '' | ||
+ | }, | ||
+ | $window = $(window), | ||
+ | $document = $(document), | ||
+ | sticked = [], | ||
+ | windowHeight = $window.height(), | ||
+ | scroller = function() { | ||
+ | var scrollTop = $window.scrollTop(), | ||
+ | documentHeight = $document.height(), | ||
+ | dwh = documentHeight - windowHeight, | ||
+ | extra = (scrollTop > dwh) ? dwh - scrollTop : 0; | ||
+ | |||
+ | for (var i = 0; i < sticked.length; i++) { | ||
+ | var s = sticked[i], | ||
+ | elementTop = s.stickyWrapper.offset().top, | ||
+ | etse = elementTop - s.topSpacing - extra; | ||
+ | |||
+ | if (scrollTop <= etse) { | ||
+ | if (s.currentTop !== null) { | ||
+ | s.stickyElement | ||
+ | .css('position', '') | ||
+ | .css('top', ''); | ||
+ | s.stickyElement.parent().removeClass(s.className); | ||
+ | $("#top_menu_14").removeClass("opaque").addClass("transparent"); | ||
+ | $("#top_menu_14").stop().fadeTo(250, 0.2); | ||
+ | s.currentTop = null; | ||
+ | } | ||
+ | } | ||
+ | else { | ||
+ | var newTop = documentHeight - s.stickyElement.outerHeight() | ||
+ | - s.topSpacing - s.bottomSpacing - scrollTop - extra; | ||
+ | if (newTop < 0) { | ||
+ | newTop = newTop + s.topSpacing; | ||
+ | } else { | ||
+ | newTop = s.topSpacing; | ||
+ | } | ||
+ | if (s.currentTop != newTop) { | ||
+ | s.stickyElement | ||
+ | .css('position', 'fixed') | ||
+ | .css('top', newTop); | ||
+ | |||
+ | if (typeof s.getWidthFrom !== 'undefined') { | ||
+ | s.stickyElement.css('width', $(s.getWidthFrom).width()); | ||
+ | } | ||
+ | |||
+ | s.stickyElement.parent().addClass(s.className); | ||
+ | $("#top_menu_14").removeClass("transparent").addClass("opaque"); | ||
+ | $("#top_menu_14").stop().fadeTo(250, 1); | ||
+ | s.currentTop = newTop; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | }, | ||
+ | resizer = function() { | ||
+ | windowHeight = $window.height(); | ||
+ | }, | ||
+ | methods = { | ||
+ | init: function(options) { | ||
+ | var o = $.extend(defaults, options); | ||
+ | return this.each(function() { | ||
+ | var stickyElement = $(this); | ||
+ | |||
+ | stickyId = stickyElement.attr('id'); | ||
+ | wrapper = $('<div></div>') | ||
+ | .attr('id', stickyId + '-sticky-wrapper') | ||
+ | .addClass(o.wrapperClassName); | ||
+ | stickyElement.wrapAll(wrapper); | ||
+ | |||
+ | if (o.center) { | ||
+ | stickyElement.parent().css({width:stickyElement.outerWidth(),marginLeft:"auto",marginRight:"auto"}); | ||
+ | } | ||
+ | |||
+ | if (stickyElement.css("float") == "right") { | ||
+ | stickyElement.css({"float":"none"}).parent().css({"float":"right"}); | ||
+ | } | ||
+ | |||
+ | var stickyWrapper = stickyElement.parent(); | ||
+ | stickyWrapper.css('height', stickyElement.outerHeight()); | ||
+ | sticked.push({ | ||
+ | topSpacing: o.topSpacing, | ||
+ | bottomSpacing: o.bottomSpacing, | ||
+ | stickyElement: stickyElement, | ||
+ | currentTop: null, | ||
+ | stickyWrapper: stickyWrapper, | ||
+ | className: o.className, | ||
+ | getWidthFrom: o.getWidthFrom | ||
+ | }); | ||
+ | }); | ||
+ | }, | ||
+ | update: scroller | ||
+ | }; | ||
+ | |||
+ | // should be more efficient than using $window.scroll(scroller) and $window.resize(resizer): | ||
+ | if (window.addEventListener) { | ||
+ | window.addEventListener('scroll', scroller, false); | ||
+ | window.addEventListener('resize', resizer, false); | ||
+ | } else if (window.attachEvent) { | ||
+ | window.attachEvent('onscroll', scroller); | ||
+ | window.attachEvent('onresize', resizer); | ||
+ | } | ||
+ | |||
+ | $.fn.sticky = function(method) { | ||
+ | return methods.init.apply( this, arguments ); | ||
+ | }; | ||
+ | $(function() { | ||
+ | setTimeout(scroller, 0); | ||
+ | }); | ||
+ | })(jQuery); | ||
$("#menuContainer li").hover( | $("#menuContainer li").hover( | ||
Line 37: | Line 154: | ||
}); | }); | ||
− | + | $("#menuContainer").sticky({topSpacing:16}); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | }); | + | |
$("#top_menu_14").css("opacity", "0.2"); | $("#top_menu_14").css("opacity", "0.2"); | ||
$("#top_menu_14").mouseenter(function(e){ | $("#top_menu_14").mouseenter(function(e){ | ||
− | $(" | + | $(this).not(".opaque").stop().fadeTo(500, 1); |
}); | }); | ||
$("#top_menu_14").mouseleave(function(){ | $("#top_menu_14").mouseleave(function(){ | ||
− | $(" | + | $(this).not(".opaque").stop().fadeTo(500, 0.2); |
}); | }); | ||
Line 176: | Line 285: | ||
} | } | ||
− | . | + | .opaque { |
− | + | opacity: 1; | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
Revision as of 02:12, 22 May 2015