简体   繁体   中英

how to add drag functionality to chrome extension?

This is a general question, but how can you add the drag feature to a custom chrome extension.

I'm building a custom chrome ext. but I would like the user to drag it around; currently it's fixed at the right corner.

I would like it to be similar to "Moat" extension (if you're familiar w/ it). Here is a screenshot of this ext. and URL .

在此处输入图片说明

Is it simply adding Jquery UI, or do i need to do something special?

I tried to set the width and height 100% to the body element, so it would take the whole page and add a div inside the body , which is the ext, and set a z-index to 1, but this approach is not working out. The body width and height is not expanding, and the extension is still fixed at the top right hand corner.

I tried to inspect the element and see if I can find a way to 'detach' from that corner but had no luck with it.

Has anybody had experience with chrome extension and adding a drag feature? Again, this is a general question. Your help will be appreciated.

You actually could use jQuery UI in a couple of different ways. Of course, the Dialog Widget is great, but if you only want to drag things around the screen, you can use Draggable . You will find a working example at the link.

Once you add the jQuery UI, it is simply one line:

$('#yourSelectionID').draggable();

For example:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Draggable - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <style>
  #draggable { width: 150px; height: 150px; padding: 0.5em; }
  </style>
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#draggable" ).draggable();
  } );
  </script>
</head>
<body>

<div id="draggable" class="ui-widget-content">
  <p>Drag me around</p>
</div>


</body>
</html>

The above code was taken from the link above. Please note to include jQuery.js and the jQuery-ui.js files to make this work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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