1.首先封装了一个插件:

(function($) {
var Anywhere = {
DEF_DURATION: 1000,
ATTR_DURATION: "duration",
ATTR_ANYTYPE: "anytype",
$body_html: $("body, html"),
$window: $(window)
};
$.fn.extend({
toAnywhere: function(duration) {
return $(this).each(function() {
new _anyWhere(this, duration).getAnyWhere();
});
}
});

function _anyWhere(el, duration) {
	this.el = el;
	this.$el = $(el);
	this.duration = parseInt(duration) || Anywhere.DEF_DURATION;
	this.anyType = this.$el.attr(Anywhere.ATTR_ANYTYPE);
}
_anyWhere.prototype = {
	build: function() {
		if(!/MSIE 6/.test(navigator.userAgent)) {
			this.$el.css("position", "fixed");
		}
		this.$el.css("display", "none");
	},
	getAnyWhere: function() {
		if(this.anyType != "link") {
			this.build();
			this.bindWindowScroll();
		}
		this.bindClick();
	},
	bindClick: function() {
		var duration = this.duration;
		this.$el.click(function() {
			var $this = $(this);
			Anywhere.$body_html.animate({
				scrollTop: $($this.attr("href")).offset().top + "px"
			}, parseInt($this.attr("duration")) || duration);
			return false;
		});
	},
	bindWindowScroll: function() {
		var $this = this.$el;
		Anywhere.$window.bind("scroll", function() {
			$(this).scrollTop() > 50 ? $this.fadeIn("fast") : $this.hide();
		});
	}
}

})(jQuery);

只需要调用就行了$(".to_any_link").toAnywhere(500);

相关文章:

  • 2022-01-10
  • 2021-11-10
  • 2022-01-05
  • 2022-12-23
  • 2021-08-06
  • 2021-10-30
  • 2022-12-23
  • 2021-09-14
猜你喜欢
  • 2022-12-23
  • 2022-01-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-26
  • 2021-07-28
相关资源
相似解决方案