1

What I want is really simple, but every time I try to add the functionality I want, the more I'd mess things up, so I decided to ask help and stick with the working basic script I have now.

I already have a script in progress, that I would like to develop to work almost exactly like this: https://stackoverflow.com/a/7133084/1399030 { http://jsfiddle.net/Paulpro/YpeeR/25/ } (by: PaulP.R.O.)

  • Open a hidden span
  • Hide a hidden span
  • Span has "CLOSE" button to exit span
  • Hide currently opened span when another span is triggered

Think... Image Gallery Preview functionality... Kind of.

"Preview" spans are triggered when either .popCover or a.thumbnail is clicked on the webpage, this hidden span will appear based on its specified unique id, by jQuery inserting display: block; to its css.
This is inside a loop with multiple items.

I've gotten this far and this is the working script that I use:

$(document).ready(function() {

    $('.popCover').click(function(){ 
     divID = $(this).attr('id');
        $("#tooltip-"+divID).fadeIn('5000', function() {
        $("#tooltip-"+divID).css("display", "block");
        });
    });

    $("a.thumbnail").click(function() {
     dvID = $(this).attr('id');
        $("#tooltip-"+dvID).fadeIn('5000', function() {
        $("#tooltip-"+dvID).css("display", "block");
        });
    });


});

But now, I need to add to these functions the trigger to make the span disappear again, (by inserting display: none; to its css.

I'd want the CURRENT SPAN to disappear when:
01. Mouse click is made outside of the span element
02. An exit or X button is clicked INSIDE the span. (like on image galleries, when they preview an image, and exit it by either clicking outside the element or an exit button provided within the preview)
03. .popCover or a.thumbnail is re-clicked (probably to trigger another span of a different ID to show.)

NOTES:
Currently, I can click as many anchors on the page and all these spans with different IDs just accumulate and stack up over each other on the page. I don't really want that. I don't want more than 1 span to be open at one time, so I was hoping to add functionality that would make the current opened span exit itself when another anchor click is made.

I really did try to do this myself, but... I can't get the methods I've tried to work. It was too complicated to add all these functions together since I'm no jQuery expert. I could get one to work and then ruin it by trying to work in another.

Also, I was thinking of using this similar way of exiting the span:

$(".the_span").fadeOut("5000").css("display", "none");

The only reason I'm not willing to just use some plugin and uncomplicate things for me is, I already really like my "Preview" span css, I have it all ready. I just need the jquery part to work:

To display: block a span when triggered, and display: none it if mentioned conditions are met.

Hoping for assistance, and will be very grateful for each single one! Thank you.

Community
  • 1
  • 1
Mafia
  • 792
  • 2
  • 19
  • 39

2 Answers2

0

You have to try to add a class on the opened / active element and then bind all the events to close it. Binds have to be done on elements with class .active for example, when closed, .active class have to be removed.

acidghost
  • 484
  • 4
  • 14
0

I've finally gotten this to work! :o)

By using if ($("span.the_span").is(":visible")) to check if span with class="the_span" was currently visible / open / or has display: block in its CSS, and if so, to:
- hide the currently open span, before proceeding to show the new span. -

Here's my working finished product that addresses all the functionality I wanted:

   $(document).ready(function() {

   // When clicks on either ".popCover" or "a.thumbnail" is made,
   // Funcion clickPOP is triggered:
   var clickPOP = function() {
        divID = $(this).attr('id'); 

        // Checks if "span.the_span" is already currently open:
        if ($("span.the_span").is(":visible")) {
        $("span.the_span").css("display", "none"); // If open, this is where it closes it..
        $("#tooltip-"+divID).fadeIn('200', function() { // Then, proceeds to open the new clicked span here.
                $("span.the_span #tooltip-"+divID).css("display", "block"); });
              }

         // But if no "span.the_span" is currently open:
         // No need to close anything, it will directly open the new span...
         else { 
            $("#tooltip-"+divID).fadeIn('5000', function() {
                $("span.the_span #tooltip-"+divID).css("display", "block"); });
                }
  } // End of Function. Added functionality starts below...

        // Exits "span.the_span" when mouse clicks outside of element
        // ... ("Outside of element" means: outside of "span.the_span")
        $(document).click(function(){
        $("span.the_span").css("display", "none");
        });

        // Exit Button : Exits "span.the_span" when exit button is clicked
        $('span.exitme').css('cursor', 'pointer').click(function(e){
        $("span.the_span").css("display", "none");
        e.stopPropagation();
        });

        // This makes sure that clicks inside "span.the_span" continue to work
        $('span.the_span').click(function(e){
        e.stopPropagation();
        });

        // This makes sure that clicks on ".popCover" continue to work
        $(".popCover").click(function(e){
        e.stopPropagation();
        });

        // This makes sure that clicks on "a.thumbnail" continue to work
        $("a.thumbnail").click(function(e){
        e.stopPropagation();
        });

// Clicks on both ".popCover" & "a.thumbnail"
// ... will trigger actions specified on function: clickPOP.
$(".popCover").click(clickPOP);
$("a.thumbnail").click(clickPOP);

    });

As you can see, I've also added the $(document).click(function() etc. to get my original desired functionality of hiding the span when mouse clicks outside of the element, but making sure that clicks can still be made if they are done on .popCover (div) or a.thumbnail (link) on the webpage.

Also, I wouldn't have been able to complete writing this method without the tips from these posts:
* Running same function / different triggers: https://stackoverflow.com/a/1191837/1399030
* Fix clicking inside element (including exit button): https://stackoverflow.com/a/4660691/1399030
* How to check if something is hidden or visible: https://stackoverflow.com/a/178450/1399030

I especially found the last post VERY helpful (and basically it made me understand what I was doing), because poster: Tsvetomir Tsonev included in his code comments:

// Checks for display:[none|block], ignores visible:[true|false]"

I didn't really initially understand that jQuery was able to check or connect with CSS that wasn't inline (being a jQuery noob myself), so that post was indeed very enlightening.


Of course, if there is a better, more efficient way to do this, I would be very happy to be enlightened some more! jQuery is still a learning curve for me, and I'm a very eager student!

Community
  • 1
  • 1
Mafia
  • 792
  • 2
  • 19
  • 39