0

I am using scrollTop() event to show/hide div in certain position also I am using onClick() event to permanently hide div, the event is working good but the problem is that when i scrolling page then the div showing again. My code is as under.

Javascript code

<script type="text/javascript">
    $(document).scroll(function() {
        if (screen.width > 768) {
            var y = $(this).scrollTop();
            if (y < 500) {
                $('.benchdiv').fadeOut();
            }
            else {
                $('.benchdiv').fadeIn();
            }
        }
    });
</script>
<script type="text/javascript">
    $(document).ready(function(){
        $(".removebg").click(function(){
            $(".benchdiv").hide();
        });
    });
</script>

HTML Code

<div class="benchdiv">
    <a href="http://www.benchmarkemail.com?p=636276" target=_blank><img src="//www.benchmarkemail.com/images/partner/banners/125x125.gif" width="125" height="125" border="0" /></a>
    <div class="benchremove"><a href="javascript:void(0);" class="removebg glyphicon glyphicon-remove"></a></div>
 </div>
1
  • Try using a bool that indicates if you have enter on ".removebg" function and check it before fadeIn the div Commented Jul 7, 2016 at 11:57

2 Answers 2

1

Use

$(".benchdiv").remove();

instead of

$(".benchdiv").hide();

to permanently remove the div.

Works?

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

Comments

0

You can either Remove the element:

$(".benchdiv").remove();

or if you will use it later you can do something like:

<script type="text/javascript">
var clicked = false;
    $(document).scroll(function() {
  if(!clicked){
        if (screen.width > 768) {
            var y = $(this).scrollTop();
            if (y < 500) {
                $('.benchdiv').fadeOut();
            }
            else {
                $('.benchdiv').fadeIn();
            }
        }
      }
    });
</script>
<script type="text/javascript">
    $(document).ready(function(){
        $(".removebg").click(function(){
            $(".benchdiv").hide();
            clicked = true;
        });
    });
</script>

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.