Last active
December 17, 2015 18:59
-
-
Save tlimpanont/5656747 to your computer and use it in GitHub Desktop.
display element depend on mousmove X cordinate.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Document</title> | |
</head> | |
<body> | |
<div class="head_images" style="width: 891px;"> | |
<img src="images/bryan_vink_1.jpg"/> | |
<img src="images/bryan_vink_2.jpg"/> | |
<img src="images/bryan_vink_3.jpg"/> | |
<img src="images/bryan_vink_4.jpg"/> | |
</div> | |
<script type="text/javascript"> | |
jQuery(function() { | |
var switcher = jQuery(".head_images").mousemove_switch({ | |
switchItems: jQuery(".head_images img") | |
}); | |
switcher.instance().start(); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($, window, document, undefined) { | |
var pluginName = "mousemove_switch", | |
defaults = { | |
switchItems: jQuery("body") | |
}; | |
function Plugin(element, options) { | |
this.element = element; | |
this.options = $.extend({}, defaults, options); | |
this._defaults = defaults; | |
this._name = pluginName; | |
this.init(); | |
} | |
Plugin.prototype = { | |
init: function () { | |
}, | |
start: function () { | |
var total_switch_items = this.options.switchItems.size(); | |
var container_width = jQuery(this.element).width(); | |
var per_item_space = container_width / total_switch_items; | |
var switchItems = this.options.switchItems; | |
switchItems.filter(":gt(0)").hide(); | |
jQuery(this.element).on("mousemove", function (e) { | |
var x = e.pageX - jQuery(this).offset().left; | |
var y = e.pageY - jQuery(this).offset().top; | |
var showIndex = calculateIndex(x); | |
console.log(showIndex); | |
switchItems.filter(":eq(" + showIndex + ")").show(); | |
switchItems.not(":eq(" + showIndex + ")").hide(); | |
}); | |
function calculateIndex(x) { | |
var ratio = x / container_width; | |
var index = Math.ceil(((ratio * container_width) / per_item_space)) - 1; | |
if (index > total_switch_items - 1) { | |
return total_switch_items - 1 | |
} | |
if (index < 0) { | |
return 0; | |
} | |
return index; | |
} | |
}, | |
stop: function () { | |
jQuery(this.element).off("mousemove"); | |
} | |
}; | |
$.fn[pluginName] = function (options) { | |
return $.extend(this.each(function () { | |
}), { | |
instance: function () { | |
if (!$.data(this, "plugin_" + pluginName)) { | |
$.data(this, "plugin_" + pluginName, new Plugin(this, options)); | |
} | |
return $.data(this, "plugin_" + pluginName) | |
} | |
}); | |
}; | |
})(jQuery, window, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment