0

Hi Friends need your help I trying to develop UI for cart. I need to display cart when user click on buy button cart will show for two second then hide. Now the thing is if user mouseover on the open cart its will remain open once user mouseout from the cart for basic demo you can go to this link http://jsfiddle.net/uVdb3/5/ or you can see my code below

html

<table width="100%">
  <tr>
    <td width="51%" align="left" valign="bottom"><a href="#" class="buyBtn">Buy Now</a></td>
    <td width="49%" align="left" valign="top"></td>
  </tr>
</table>

<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>


<div class="cart">
            <div class="number">0</div>


    <div class="cartView">

                <table width="100%" class="itmCart">

                        <td width="16%" class="item"><img src="images/cartPrdView.png" width="55" height="73" alt=" "></td>
                        <td width="71%">Anniversasdfry Card Pink Rose X 1: Rs 149/-</td>
                        <td width="13%" align="center"><a href="#" class="delRow"><img src="images/cross-script.png" width="16" height="16" alt=" "></a></td>
                    </tr>
                </table>


                <table width="100%" class="price">
  <tr>
    <td width="50%">Item Cost</td>
    <td align="right">Rs 649.00 /-</td>

  </tr>
  <tr>
    <td>Shipping Cost</td>
    <td align="right">Rs 30.00 /-</td>
  </tr>
  <tr>
    <td><span>Amount Payable</span></td>
    <td align="right"><span>Rs 679.00 /-</span></td>
  </tr>

              </table>
                <table width="100%" class="btnCont">
                    <tr>
                        <td align="center"><a href="#" id="CrtBtn"> Show cart</a></td>
                        <td align="center"><a href="#" id="CrtBtn">Pay now</a></td>
                    </tr>
                </table>



            </div>







        </div>

css

.oneItem{background:red; color:#fff; border-radius:110px; padding:0px 14px; float:left; font-weight:bold; font-size:14px; position:absolute; top:0; left:0;}

.cart{position:absolute;}
 .cart .cartView{position: absolute; background: red; width: 350px; padding: 8px; left: -5px;
 top: 30px; z-index:999; behavior: url(css/PIE.htc);  -webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius:5px; display:none;
}

script

function opencart(){

        //alert('hi')
        $('.cart').children('.cartView').slideDown(200);

    setTimeout(function(){


        $('.cart').children('.cartView').slideUp(200);

            },2000)






        }

$('.buyBtn').click(function () {

        $(this).append("<div class='oneItem'>1</div>")
        var buyPos = $(this).position();
        var buyPosL = buyPos.left;
        var buyPosT = buyPos.top;

        var CartPos = $('.cart .number').offset();
        var CartPosL = CartPos.left;
        var CartPosT = CartPos.top;

        $('.oneItem').css({
            left: buyPosL + 'px',
            top: buyPosT + 'px'
        })

        $('.oneItem').animate({
            left: CartPosL + 'px',
            top: CartPosT + 'px'
        }, 1000, function () {

            $(this).remove();
            opencart();
        })



    })

In this code my cart opens in red color code working perfact but cart close aftre 2 seconds. How to stop cart to get close when user mouse over the cart and when user mouse out from the cart it get close

Please help me guys thanks in advance.. :)

1

4 Answers 4

1

HEre jsfiddle http://jsfiddle.net/uVdb3/6/

I just check if hover then run setTimeout again else slideUp.

    function test(){
    setTimeout(function(){
        if($('.cart').children('.cartView').is(':hover')){
            test();
            return false
        }

        $('.cart').children('.cartView').slideUp(200);}
      ,2000);

}
Sign up to request clarification or add additional context in comments.

Comments

0
 $('.buyBtn').click(function () {
    // ... Your Code
    //Add This Code  
     $( ".buyBtn" )
           .mouseover(function() {
              $('.cart').children('.cartView').slideDown(200);
            })
          .mouseout(function() {
              $('.cart').children('.cartView').slideUp(200);
           });
 });

Check for DEMO

1 Comment

Thank for help nitish but my question is after clicking on buy now the red box is open when user mouse over that red box thant box will not close untill unliss user remove his mouse from that red button...
0

Use setInterval() instead of setTimeout(). Once you slideUp() the cart use clearInterval() to stop running the function on intervals.

var slid;
slid = setInterval(hide, 2000);
function hide(){
    if($('.cart').is(':hover')){
        $('.cart').children('.cartView').slideUp(200);
        clearInterval(slid);
    } else {}
}   

JSFIDDLE DEMO

1 Comment

@Kamal added a fiddle
0

Try this working solution.

function opencart(){

        //alert('hi')
        $('.cart').children('.cartView').slideDown(200);

        window.setTimeout(checkHover,2000);

}

function checkHover(){

       if(!$('.cartView').is(':hover')){
          $('.cart').children('.cartView').slideUp(200);
       }else{
               setTimeout(checkHover,2000);
       }
}

Working Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.