单击时菜单淡入有效。但是点击淡出效果不起作用

时间:2018-07-10 23:05:05

标签: jquery wordpress

我有一个“汉堡”图标作为按钮,单击该按钮可使菜单淡入视图。当我尝试将fadeOut分配给菜单中的另一个图标/按钮时,它不起作用,并且菜单无限期保持可见,除非刷新页面,否则无法将其隐藏。

jQuery代码:

public class UseConnectfragment extends Fragment {

    RecyclerView FriendsRecycler,FollowingRecylcer,FollowerRecycler;

    ArrayList<viewUserProfile_Wrapper> followersList;
    ArrayList<viewUserProfile_Wrapper> friendsList;
    ArrayList<viewUserProfile_Wrapper> followingList;

    UserConnections_Adapter adapter1;
    UserConnections_Adapter adapter2;
    UserConnections_Adapter adapter3;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.userprofile_connections,container,false);
        setRetainInstance(true);
        FriendsRecycler=view.findViewById(R.id.userConnections_recyclerFriends);
        FollowingRecylcer=view.findViewById(R.id.userConnections_recyclerFollowing);
        FollowerRecycler=view.findViewById(R.id.userConnections_recyclerFollower);
        FriendsRecycler.setHasFixedSize(true);
        FollowingRecylcer.setHasFixedSize(true);
        FollowerRecycler.setHasFixedSize(true);

        FriendsRecycler.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        FollowingRecylcer.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        FollowerRecycler.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        adapter1=new UserConnections_Adapter(friendsList);
        adapter2=new UserConnections_Adapter(followingList);
        adapter3=new UserConnections_Adapter(followersList);


        return view;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.d(TAG,"Set User Visible Running");
        if (isVisibleToUser){
            Log.d(TAG,"User Connection Tab Is Visible So Call Server Now");
            new GetConncetions().execute();
        }
    }

    public class UserConnections_Adapter extends RecyclerView.Adapter<UseConnections_fragment.UserConnections_Adapter.ViewHolder> {
        ArrayList<viewUserProfile_Wrapper> connectionList;

        public UserConnections_Adapter(ArrayList<viewUserProfile_Wrapper> connectionList) {
            this.connectionList = connectionList;
        }

        @Override
        public UserConnections_Adapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            LayoutInflater inflater= (LayoutInflater) viewGroup.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view= inflater.inflate(R.layout.userprofile_connections_singleitem,viewGroup,false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(UserConnections_Adapter.ViewHolder holder, int i) {
            final viewUserProfile_Wrapper wrapper=connectionList.get(i);
            Log.d(TAG,"Wrapper on bind "+wrapper.getUserName()+" "+connectionList.size());
            holder.userName.setText(wrapper.getUserName());
            Glide.with(getActivity()).load(wrapper.getUserImage()).into(holder.userImage);
            holder.userImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getActivity(), "User Name "+wrapper.getUserName()+" User ID "+wrapper.getID(), Toast.LENGTH_SHORT).show();
                }
            });
        }

        @Override
        public int getItemCount() {
            return connectionList.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            TextView userName;
            ImageView userImage;
            public ViewHolder(View itemView) {
                super(itemView);
                userName=itemView.findViewById(R.id.userConnection_Name);
                userImage=itemView.findViewById(R.id.userConnection_friends);
            }
        }
    }
    public class GetConncetions extends AsyncTask<Void,Void,String> {
        private static final String All_Connections="http://localhost/test/Android/getallon.php";

        BufferedReader reader;
        private ProgressDialog progressDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(getActivity());
            progressDialog.setCancelable(false);
            progressDialog.setTitle("Loading...");
            progressDialog.show();
        }
        @Override
        protected String doInBackground(Void... voids) {
            Log.d(TAG,"Starting Do in background");
           // addedInterest=new ArrayList<>();
            try {



                URL url=new URL(All_Connections);
                HttpURLConnection connection= (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");
                connection.setDoInput(true);
                connection.setDoOutput(true);
                connection.connect();
                connection.setConnectTimeout(5000);
                OutputStream outputStream=connection.getOutputStream();
                BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
                writer.write(data);
                writer.flush();
                writer.close();
                outputStream.close();

                //GET INTEREST JSON FROM SERVER
                Log.d(TAG,"Response Code "+connection.getResponseCode());
                if (connection.getResponseCode()==200) {

                    InputStream inputStream = connection.getInputStream();
                    reader = new BufferedReader(new InputStreamReader(inputStream));
                    String line;
                    StringBuffer stringBuffer = new StringBuffer();
                    while ((line = reader.readLine()) != null) {
                        stringBuffer.append(line);
                        Log.d(TAG, "Appending JSON into String Buffer");
                    }
                    Log.d(TAG,"JSON Received "+stringBuffer);
                    JSONObject jsonObject=new JSONObject(stringBuffer.toString());
                    String callResult=jsonObject.getString("status");
                    Log.d(TAG,"Server Call Result "+callResult);

                    //Make These String out of the scope so we can access this outside if statement
                    String ID,Name,Image;
                    if (callResult.equals("SUCCESS")){
                        Log.d(TAG,"Server Call Is Success");
                        followersList=new ArrayList<>();
                        followingList=new ArrayList<>();
                        friendsList  =new ArrayList<>();
                        JSONArray followerDataArray=jsonObject.getJSONArray("data1");
                        for (int i = 0; i <followerDataArray.length() ; i++) {
                            String image=followerDataArray.getJSONObject(i).getString("getmyUserImage");
                            viewUserProfile_Wrapper followerData=new viewUserProfile_Wrapper(image);
                            followersList.add(followerData);
                        }
                        JSONArray followingDataArray=jsonObject.getJSONArray("data2");
                        for (int i = 0; i <followingDataArray.length() ; i++) {

                            String image=followerDataArray.getJSONObject(i).getString("getmyUserImage");
                            viewUserProfile_Wrapper followerData=new viewUserProfile_Wrapper(image);
                            followingList.add(followerData);
                        }
                        JSONArray friendDataArray=jsonObject.getJSONArray("data3");
                        for (int i = 0; i <friendDataArray.length() ; i++) {

                            String image=followerDataArray.getJSONObject(i).getString("getmyUserImage");
                            viewUserProfile_Wrapper followerData=new viewUserProfile_Wrapper(image);
                            friendsList.add(followerData);
                        }
                        return null;
                    }


                }

                return "Success";


            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String v) {
            super.onPostExecute(v);
            Log.d(TAG,"On Post running");
            progressDialog.dismiss();
            if (v!=null){
                if (v.equals("Success")){
                    FriendsRecycler.setAdapter(adapter1);
                    FollowingRecylcer.setAdapter(adapter2);
                    FollowerRecycler.setAdapter(adapter3);

                }else {
                    Toast.makeText(getActivity(), "Server Error.Try Again", Toast.LENGTH_SHORT).show();
                }

            }



        }
    }


}

HTML代码:

jQuery(document).ready(function($){
    $(".acetrnt-toggle-button").click(function(){
    $("#acetrnt-toggle-overlay").fadeIn();

    });
});

jQuery(document).ready(function($){
    $(".acetrnt-overlay-close").click(function(){
    $("#acetrnt-toggle-overlay").fadeOut();

    });
});

CSS代码:

<div id="container">

            <button class="acetrnt-toggle-button"><icon class="fa fa-bars"></icon></button>

            <h2 class="rp-heading">Recent Posts</h2>

            <div id="acetrnt-toggle-overlay">

                <button class="acetrnt-overlay-close"><icon class="fa fa-close"></icon></button>

                <nav class="site-nav-primary">

                    <?php $args = array('theme_location' => 'primary'); ?>
                    <?php wp_nav_menu(  $args ); ?>

                </nav>

            </div>

        </div>

如果您有任何想法,我将不胜感激。

2 个答案:

答案 0 :(得分:0)

您可以使用event.stopPropagation();

jQuery(document).ready(function($){
$(".acetrnt-toggle-button").click(function(){
$("#acetrnt-toggle-overlay").fadeIn();

});
});

jQuery(document).ready(function($){
$(".acetrnt-overlay-close").click(function(event){
event.stopPropagation();
$("#acetrnt-toggle-overlay").fadeOut();

});
});

答案 1 :(得分:0)

您的js看起来不对,您不需要准备两个文档,将其替换为下面的代码

$(document).ready(function($){
  $(".acetrnt-toggle-button").click(function(){
  $("#acetrnt-toggle-overlay").fadeIn();

  });

  $(".acetrnt-overlay-close").click(function(){
  $("#acetrnt-toggle-overlay").fadeOut();

  });
});

并确保在html文件中添加jquery,检查工作示例https://codepen.io/anon/pen/MBWdbg