简体   繁体   English

如何停止使用Javascript淡出HTML元素?

[英]How to stop a HTML element fading out using Javascript?

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. 更新我有一个jsfiddle在这里显示问题: http : //jsfiddle.net/waf11s6u/1/当您在搜索栏中键入字母时,附加到div的自定义滚动条消失了。 The scrollbar may be getting faded out by the code that fades out non-matching words from the div? 滚动条可能会被淡出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. 我正在为Facebook游戏创建一个自定义的多好友选择器,它看起来类似于: http : //tinyurl.com/gus79cf用户可以在搜索栏中键入内容,并且任何匹配的朋友名称都会显示在下面的区域中。 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/ 这是插件的网站: 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. 在视觉上,滚动条由两部分组成,第一部分是轨道(我已将轨道绘制到背景图像上,因此它实际上不是Javascript代码的一部分),第二部分是图标,图标是沿着轨道上下移动的小图像。

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"). 包含好友名称和图像的div是使用Javascript动态创建的(称为“ mfsForm”)。 When the user begins typing a name, I have some Javascript that will fade out non-matching friend names & images. 当用户开始输入名称时,我有一些Javascript可以淡化不匹配的朋友名称和图像。

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"); 我认为$(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"); 我试图通过引用图标的类(mCSB_dragger_bar)并使其在此处褪色来定位图标: $(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! 非常感谢您对我可以尝试解决此问题的任何帮助或建议,在此先感谢您!

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. 输入代码onchange="f()"或onkey或其他。 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. 可能是您的脚本删除了图标(html元素)然后创建了它。

In this mode the icon will always appear on each key press. 在此模式下,每次按键时图标将始终出现。

Try $(this).find('.mCSB_dragger_bar').fadeIn("slow"); 试试$(this).find('.mCSB_dragger_bar').fadeIn("slow"); not $(this).next('.mCSB_dragger_bar').fadeIn("slow"); 不是$(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; 如果在$(this)元素上存在类名称为mCSB_dragger_bar元素($ this-> $(“#mfsForm div”)-> id为mfsForm的div元素),它将找到并显示;

NEXT return only one element after $this, may be between $(this) and mCSB_dragger_bar exist another element. NEXT仅在$ this之后返回一个元素,可能在$(this)和mCSB_dragger_bar存在另一元素。

Also try $(this).parent().find('.mCSB_dragger_bar').fadeIn("slow"); 也尝试$(this).parent().find('.mCSB_dragger_bar').fadeIn("slow"); if mCSB_dragger_bar and $(this) is on the same doom level 如果mCSB_dragger_bar和$(this)在同一个毁灭级别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM