侧边悬浮按钮,点击移动到顶部和底部

// 侧边按钮
    $(".suspension .a-top").click(function () {
        $("html,body").animate({scrollTop: 0});
        return false;
    });
    $(".suspension .a-bottom").click(function () {
        $("html,body").animate({scrollTop: $(document).height()}, 1000);
        return false;
    });
    $(window).scroll(function () {
        var st = $(document).scrollTop();
        var $top = $(".suspension .a-top,.suspension .a-bottom");
        if (st > 100) {
            $top.css({display: 'block'});
        } else {
            if ($top.is(":visible")) {
                $top.hide();
            }
        }
    })
<!-- 悬浮按钮 -->
            <div class="suspension">
                <div class="suspension-box">
                    <a href="#" class="a a-top">
                        <i class="fa fa-angle-double-up i" aria-hidden="true"></i>
                    </a>
                </div>
                <div class="suspension-box">
                    <a href="#" class="a a-bottom">
                        <i class="fa fa-angle-double-down i" aria-hidden="true"></i>
                    </a>
                </div>
            </div>
/*悬浮按钮*/
.suspension{
    position:fixed;
    z-index:55;
    bottom:85px;
    width:44px;
    height:50%;
    right: 10px;
}
.suspension-box{
    position:relative;
    float:right;
}
.suspension .a{
    text-align: center;
    display: block;
    vertical-align: middle;
    line-height: 44px;
    width:44px;
    height:44px;
    background-color:#353535;
    margin-bottom:4px;
    cursor:pointer;
    outline:none;
}

.suspension .a .i{
    width:44px;
    height:44px;
    color: #fff;
    font-size: 25px;
}

.suspension .a-top,.suspension .a-bottom{
    background:#D2D3D6;
    display:none;
}

.suspension .a-top:hover,.suspension .a-bottom:hover{
    background:#c0c1c3;
}
/*结束*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Android 中实现侧边悬浮窗需要使用 WindowManager 和 View 来实现。首先,需要在应用的 AndroidManifest.xml 文件中声明 SYSTEM_ALERT_WINDOW 权限,以允许应用在其他应用的上层显示窗口。 接下来,在应用中创建一个悬浮窗口的 View,并且将其添加到 WindowManager 中。可以使用 WindowManager.LayoutParams 来设置悬浮窗口的位置、大小、透明度等属性。 同时,需要添加悬浮窗口的触摸事件,以便用户可以通过拖动悬浮窗口来移动它的位置。 最后,需要在应用退出时及时移除悬浮窗口,以避免在后台运行时占用系统资源。 以下是一个简单的示例代码,可以在应用中创建一个侧边悬浮窗口: ```java public class FloatingViewService extends Service implements View.OnTouchListener { private WindowManager mWindowManager; private View mFloatingView; private int mInitialX; private int mInitialY; private float mInitialTouchX; private float mInitialTouchY; public FloatingViewService() { // Empty constructor required for service. } @Override public IBinder onBind(Intent intent) { // Not used return null; } @Override public void onCreate() { super.onCreate(); // Create a new view for the floating window mFloatingView = LayoutInflater.from(this).inflate(R.layout.floating_view, null); // Set the touch listener on the floating view mFloatingView.setOnTouchListener(this); // Add the floating view to the window manager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE); WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); layoutParams.gravity = Gravity.CENTER | Gravity.START; layoutParams.x = 0; layoutParams.y = 0; mWindowManager.addView(mFloatingView, layoutParams); } @Override public void onDestroy() { super.onDestroy(); // Remove the floating view from the window manager when the service is destroyed if (mFloatingView != null) { mWindowManager.removeView(mFloatingView); } } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // Save the initial touch position and floating view position mInitialX = layoutParams.x; mInitialY = layoutParams.y; mInitialTouchX = event.getRawX(); mInitialTouchY = event.getRawY(); return true; case MotionEvent.ACTION_UP: // Do nothing on touch up return true; case MotionEvent.ACTION_MOVE: // Calculate the new position of the floating view based on the touch position layoutParams.x = mInitialX + (int) (event.getRawX() - mInitialTouchX); layoutParams.y = mInitialY + (int) (event.getRawY() - mInitialTouchY); mWindowManager.updateViewLayout(mFloatingView, layoutParams); return true; } return false; } } ``` 需要注意的是,在 Android 8.0 及以上版本中,需要使用 TYPE_APPLICATION_OVERLAY 来显示悬浮窗口。而在 Android 6.0 及以上版本中,需要动态请求 SYSTEM_ALERT_WINDOW 权限。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值