Created
May 22, 2015 02:15
-
-
Save hnq90/9b6fdf27c677f8b71313 to your computer and use it in GitHub Desktop.
Images Protector
This file contains hidden or 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
| $.fn.protectImage = (settings) -> | |
| settings = jQuery.extend( | |
| zIndex: 10 | |
| offset: 0 | |
| , settings) | |
| @each -> | |
| thisImg = $(this) | |
| position = thisImg.offset() | |
| height = thisImg.height() | |
| width = thisImg.width() | |
| srcimg = this | |
| protect = $("<div>") | |
| protect.css({ | |
| width: width | |
| height: height | |
| top: position.top | |
| left: position.left - settings.offset | |
| position: "absolute" | |
| zIndex: settings.zIndex | |
| }) | |
| .addClass("image-protector") | |
| if $(srcimg).parent().get(0).tagName is "A" | |
| link = $(srcimg).parent().clone() | |
| link.html protect | |
| link.appendTo thisImg.parent() | |
| else | |
| $(protect).appendTo thisImg.parent() | |
| $.fn.protectImage2 = (settings) -> | |
| settings = jQuery.extend( | |
| zIndex: 10 | |
| , settings) | |
| @each -> | |
| thisImg = $(this) | |
| height = thisImg.height() | |
| width = thisImg.width() | |
| title = thisImg.attr("alt") | |
| currentStyle = thisImg.attr("style") | |
| thisImg.wrap "<div></div>" | |
| .parent() | |
| .css({ | |
| display: "block" | |
| width: width | |
| height: height | |
| background: "url(" + thisImg.attr("src") + ") no-repeat" | |
| zIndex: settings.zIndex | |
| }) | |
| .addClass thisImg.attr("class") | |
| .addClass "image-protector" | |
| .removeClass "protect" | |
| .attr "title", title | |
| if currentStyle && currentStyle.length > 0 | |
| newStyle = thisImg.parent().attr("style"); | |
| thisImg.parent().attr("style", newStyle + currentStyle) | |
| thisImg.css display: "none" | |
| $.fn.reloadProtector = (settings) -> | |
| settings = jQuery.extend( | |
| offset: 0 | |
| , settings) | |
| selector = $(this) | |
| $(".image-protector").remove() | |
| $(selector).protectImage({offset: settings.offset}) | |
| $.fn.reloadProtector2 = -> | |
| @each -> | |
| selector = $(this) | |
| selector.parent().replaceWith -> | |
| $ "img", this | |
| selector.protectImage2() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment