Skip to content

Instantly share code, notes, and snippets.

@mfiyalka
Created March 3, 2018 11:26
Show Gist options
  • Select an option

  • Save mfiyalka/9b52cc1deac1afe3cf71c63ef82af0be to your computer and use it in GitHub Desktop.

Select an option

Save mfiyalka/9b52cc1deac1afe3cf71c63ef82af0be to your computer and use it in GitHub Desktop.
Show header dropdown
// main.php
...
<?php Pjax::begin(['id' => 'cart_widget']) ?>
<?= CartWidget::widget() ?>
<?php Pjax::end() ?>
...
// віджет
...
<div class="header-wrapicon2">
<img src="images/icons/icon-header-02.png" class="header-icon1 js-show-header-dropdown" alt="ICON">
<span class="header-icons-noti"><?= $cart->getAmount() ?></span>
<!-- Header cart noti -->
<div class="header-cart header-dropdown">
<ul class="header-cart-wrapitem">
<?php foreach ($cart->getItems() as $item): ?>
<?php
$product = $item->getProduct();
$modification = $item->getModification();
$url = Url::to(['/shop/catalog/product', 'id' => $product->id]);
?>
<li class="header-cart-item">
<div class="header-cart-item-img">
<?php if ($product->mainPhoto): ?>
<img src="<?= $product->mainPhoto->getThumbFileUrl('file', 'cart_widget_list') ?>" alt="IMG" />
<?php endif; ?>
</div>
<div class="header-cart-item-txt">
<a href="<?= $url ?>" class="header-cart-item-name">
<?= Html::encode($product->name) ?>
</a>
<?php if ($modification): ?>
<small><?= Html::encode($modification->name) ?></small>
<?php endif; ?>
<span class="header-cart-item-info">
<?= $item->getQuantity() ?> x $<?= PriceHelper::format($item->getCost()) ?>
</span>
</div>
</li>
<?php endforeach ?>
</ul>
<?php $cost = $cart->getCost(); ?>
<div class="header-cart-total">
<strong>Sub-Total:</strong> $<?= PriceHelper::format($cost->getOrigin()) ?><br>
<?php foreach ($cost->getDiscounts() as $discount): ?>
<strong><?= Html::encode($discount->getName()) ?>:</strong> $<?= PriceHelper::format($discount->getValue()) ?><br>
<?php endforeach; ?>
<strong>Total:</strong> $<?= PriceHelper::format($cost->getTotal()) ?>
</div>
<div class="header-cart-buttons">
<div class="header-cart-wrapbtn">
<!-- Button -->
<a href="<?= Url::to(['/shop/cart/index']) ?>" class="flex-c-m size1 bg1 bo-rad-20 hov1 s-text1 trans-0-4">
Корзина
</a>
</div>
<div class="header-cart-wrapbtn">
<!-- Button -->
<a href="#" class="flex-c-m size1 bg1 bo-rad-20 hov1 s-text1 trans-0-4">
Check Out
</a>
</div>
</div>
</div>
</div>
// main.js
(function ($) {
"use strict";
/*[ Show header dropdown ]
===========================================================*/
// $('.js-show-header-dropdown').on('click', function(){
// $(this).parent().find('.header-dropdown');
// });
$(document).on('click', '.js-show-header-dropdown', function(){
$(this).parent().find('.header-dropdown');
});
var menu = $('.js-show-header-dropdown');
var sub_menu_is_showed = -1;
for(var i=0; i<menu.length; i++){
//$(menu[i]).on('click', function(){ // понял
$(document).on('click', menu[i], function(){
if(jQuery.inArray( this, menu ) == sub_menu_is_showed){
$(this).parent().find('.header-dropdown').toggleClass('show-header-dropdown');
sub_menu_is_showed = -1;
}
else {
for (var i = 0; i < menu.length; i++) {
$(menu[i]).parent().find('.header-dropdown').removeClass("show-header-dropdown");
}
$(this).parent().find('.header-dropdown').toggleClass('show-header-dropdown');
sub_menu_is_showed = jQuery.inArray( this, menu );
}
});
}
$(".js-show-header-dropdown, .header-dropdown").click(function(event){
event.stopPropagation();
});
$(window).on("click", function(){
for (var i = 0; i < menu.length; i++) {
$(menu[i]).parent().find('.header-dropdown').removeClass("show-header-dropdown");
}
sub_menu_is_showed = -1;
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment