2

UPDATE I have a jsfiddle showing the issue here: http://jsfiddle.net/waf11s6u/1/ When you type a letter into the search bar, the custom scrollbar attached to the div disappears. The scrollbar may be getting faded out by the code that fades out non-matching words from the div?

~~

I’m creating a custom multi-friend selector for a Facebook game, it looks similar to this: http://tinyurl.com/gus79cf The user can type into a search bar and any matching friend names appear in the area below. I’m using a custom scrollbar plugin to design the scrollbar for scrolling down through the list of friends. This is the plugin’s site: http://manos.malihu.gr/jquery-custom-content-scroller/

Visually the scrollbar is made up of two parts, the first is the track (I’ve drawn the track onto the background image, so it’s not actually part of the Javascript code), and the second part is the icon, the icon is the small image that moves up and down along the track.

The scrollbar works perfectly (meaning that the icon slides up and down correctly), except for one thing, whenever the user types a letter into the search bar the icon disappears, and it only becomes visible again when the search bar is empty.

The div which contains the names & images of friends is created dynamically in Javascript (it's called "mfsForm"). When the user begins typing a name, I have some Javascript that will fade out non-matching friend names & images.

I think that this code is also causing the icon to disappear.

This is the code in question:

  // Earlier code here connects to Facebook's API.
  // Then get the list of friends for this user with the Graph API
            FB.api('/me/invitable_friends?limit=48', function(response) {
                var container = document.getElementById('mfs');
  // Creating the div "mfsForm" (this will hold the friend names & photos, and is also what the custom scrollbar is applied to.)
                    var mfsForm = document.createElement('form');
                    mfsForm.id = 'mfsForm';
                    mfsForm.className = " mCustomScrollbar mfsForm";

                // Iterate through the array of friends object and create a checkbox for each one.
                for (var i = 0; i < response.data.length; i++) { //Math.min(response.data.length, 10)

                    var friendItem = document.createElement('div');  
                    friendItem.id = 'friend_' + response.data[i].id;
                    friendItem.style.cssText="width:100px; height:100px; padding:7px; color:#FFF;"
                    friendItem.style.cssFloat="left";
                    friendItem.innerHTML = '<input type="checkbox" name="friends" value="' + response.data[i].id + '" />';

                    var img = document.createElement('img');
                    img.src = response.data[i].picture.data.url;
                    img.style.cssText = 'width: 70px;height: 70px;'
                    friendItem.appendChild(img);

                    var labelName = document.createElement('label');
                    labelName.style.cssText = 'font-size: 14px;'
                    labelName.innerHTML = response.data[i].name;
                    friendItem.appendChild(labelName);

                    mfsForm.appendChild(friendItem);
                }
                container.appendChild(mfsForm);
                console.log(mfsForm);               
                $(mfsForm).mCustomScrollbar();

                // Create a button to send the Request(s)
                var sendButton = document.createElement('div');
                sendButton.id = 'sendButton';
                sendButton.onclick = sendRequest;
                container.appendChild(sendButton);

                $("#filter").keyup(function(){

        // Retrieve the input field text and reset the count to zero
        var filter = $(this).val()//, count = 0;

        // Loop through the comment list
        $("#mfsForm div").each(function(){

            // If the list item does not contain the text phrase fade it out
            if ($(this).text().search(new RegExp(filter, "i")) < 0) {
                $(this).fadeOut("slow");


            // Show the list item if the phrase matches and increase the count by 1
            } else {
                $(this).show();

                //Attempting to fade in the icon here:
                $(this).next('.mCSB_dragger_bar').fadeIn("slow"); 

                }
            });
        })
                });

I think that $(this).fadeOut("slow"); is making the scrollbar icon fade out. I've tried to target the icon by referencing its class (mCSB_dragger_bar) and fading it in here: $(this).next('.mCSB_dragger_bar').fadeIn("slow"); but it's not working.

Any help or suggestions on what I could try to fix this problem would be really appreciated, thank you in advance!

Emily
  • 1,151
  • 4
  • 21
  • 42
  • A jsFiddle, perhaps? –  Jan 26 '16 at 12:34
  • Hi @rac thanks for your reply, I'll work on making a jsfiddle now, it might take some time for me to get it working but I'll let you know when I do. – Emily Jan 26 '16 at 13:18
  • Hi again @rac I have this fiddle here: http://jsfiddle.net/waf11s6u/1/ the area inside the green box is where the friends would be listed, for this example I've just put sample text in there (because I couldn't display the friends from facebook). As you can see there is a custom scrollbar attached to the box. If you type "T" into the search bar then the scrollbar fades out, if you continute to type "Th" then the text will remain because it matches what's in the box, but if you type "Thz" then the sample text will disappear. – Emily Jan 26 '16 at 13:59
  • @rac This means that the search bar is working correctly, if you clear the search bar then the scrollbar will reappear. You may need to click "Run" several times when testing it because I've noticed some inconsistant results when testing with the search bar on jsfiddle, I think that might be down to their interface though. – Emily Jan 26 '16 at 14:00
  • @rac I think the scrollbar may be getting faded out by the same code that's supposed to check the search bar input and fade out non-matching words, if this is the case then I'm unsure of how to solve this problem, thanks in advance for looking at this. – Emily Jan 26 '16 at 14:02

2 Answers2

0

What is the problem? You do not show normal code to see where your script delete icon and i can say you to force your script to display this icon. Put to input the code onchange="f()" or onkey pres or other. And

<script>
function f(){ //$('#icon') the element witch contain icon that disapear
$('#icon').css('visibility','visible').css('display','block');
$('#icon').attr('background','url('/icon.png')')}`
/*$('#parent-of-icon').appendChild(icon );*/

And other depend why the icon disapear.
May be your script delete the icon (html element) then create it.

In this mode the icon will always appear on each key press.

Michael
  • 1,089
  • 1
  • 11
  • 28
  • Hi @mitch thanks for your reply, the code that makes the icon disappear is here: `` $(this).fadeOut("slow");`` this line fades out the non-matching friends from the list correctly, but it's also fading out the scrollbar icon that is attached to the form containing the friend list. The icon fades out, meaning it still exists but just isn't visible, therefore it doesn't need to be re-created, I'm instead trying to fade it back in with ``$(this).next('.mCSB_dragger_bar').fadeIn("slow");`` – Emily Jan 26 '16 at 13:15
0

Try $(this).find('.mCSB_dragger_bar').fadeIn("slow"); not $(this).next('.mCSB_dragger_bar').fadeIn("slow");

If element with class name mCSB_dragger_bar exist on $(this) element ( $this -> $("#mfsForm div") -> some div's on element with id=mfsForm) it will find it and show;

NEXT return only one element after $this, may be between $(this) and mCSB_dragger_bar exist another element.

Also try $(this).parent().find('.mCSB_dragger_bar').fadeIn("slow"); if mCSB_dragger_bar and $(this) is on the same doom level

Michael
  • 1,089
  • 1
  • 11
  • 28