Skip to content

Instantly share code, notes, and snippets.

@iqbalrony
Created May 15, 2019 05:09
Show Gist options
  • Save iqbalrony/89c8764102e9a8be7bbabefe967e20eb to your computer and use it in GitHub Desktop.
Save iqbalrony/89c8764102e9a8be7bbabefe967e20eb to your computer and use it in GitHub Desktop.
On hover post query by ajax
(function ($) {
"use strict";
function dgm_reset_position($current, $item){
if( $current.length === 0 )
return;
var $item = $current.next('.dgm_product_item');
var tipX = $current.outerWidth();
var tipY = 0;
if( $item.length === 0 )
return;
// set default style with dynamically
$item.css({ bottom: tipY, left: tipX });
// https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect
var digirect = $item[0].getBoundingClientRect();
// Corrections if out of window
if ((digirect.x + digirect.width) > $(window).width())
tipX = -digirect.width;
if (digirect.y < 0)
tipY = digirect.y;
// Apply corrected position
$item.css({ bottom: tipY, left: tipX });
}
$.fn.dgm_product_ajax = function () {
return this.each(function () {
$(this).on({
mouseenter: function (e) {
var $self = $(this),
$dgm_post_id = $self.data('id'),
$parent = $self.closest('.dgm_products_widget_wrapper.style_3'),
$parent_before = $parent.closest('.dgm_products_widget_wrapper_before'),
$product_item = $parent.find('.dgm_product_item');
if ($product_item.length === 0) {
$.ajax({
url: digimartAjax.ajax_url,
data: {
'post_id': $dgm_post_id,
'security': digimartAjax.nonce,
action: 'dgm_product_on_hover_ajax',
},
type: 'POST',
beforeSend: function () {
},
success: function ($response) {
if ($response != "") {
$parent.append($response);
var item = $parent.find('.dgm_product_item');
dgm_reset_position($self, item);
}
},
complete: function(){
setTimeout(function(){
if( !$self.is(":hover") && $parent.find('.dgm_product_item').length && !$parent.find('.dgm_product_item').hasClass('mouseleave') ) {
$parent.find('.dgm_product_item').addClass('mouseleave');
}
}, 1000);
}
});
} else {
$product_item.removeClass('mouseleave');
dgm_reset_position($self, $product_item);
}
},
mouseleave: function () {
var $self = $(this),
$parent = $self.closest('.dgm_products_widget_wrapper.style_3'),
$product_item = $parent.find('.dgm_product_item');
if ($product_item.length > 0) {
$product_item.addClass('mouseleave');
}
}
});
});
};
$(document).ready(function () {
$(".dgm-post-thumbnail-small").dgm_product_ajax();
});
/*End document ready*/
$(window).on("load", function () {
}); // End window LODE
$(window).on("resize", function () {
}); // End window LODE
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment