UNPKG

6.11 kBMarkdownView Raw
1## Tether
2
3[![GitHub
4version](https://badge.fury.io/gh/HubSpot%2Ftether.svg)](http://badge.fury.io/gh/HubSpot%2Ftether)
5
6[Tether](http://github.hubspot.com/tether/) is a small, focused JavaScript library for defining and managing the position of user interface (UI) elements in relation to one another on a web page. It is a tool for web developers building features that require certain UI elements to be precisely positioned based on the location of another UI element.
7
8There are often situations in UI development where elements need to be attached to other elements, but placing them right next to each other in the [DOM tree](https://en.wikipedia.org/wiki/Document_Object_Model) can be problematic based on the context. For example, what happens if the element we’re attaching other elements to is fixed to the center of the screen? Or what if the element is inside a scrollable container? How can we prevent the attached element from being clipped as it disappears from view while a user is scrolling? Tether can solve all of these problems and more.
9
10Some common UI elements that have been built with Tether are [tooltips](http://github.hubspot.com/tooltip/docs/welcome), [select menus](http://github.hubspot.com/select/docs/welcome), [dropdown menus](http://github.hubspot.com/drop/docs/welcome), and [guided tours](http://github.hubspot.com/shepherd/docs/welcome). Tether is flexible and can be used to [solve](http://github.hubspot.com/tether/examples/out-of-bounds/) [all](http://github.hubspot.com/tether/examples/content-visible) [kinds](http://github.hubspot.com/tether/examples/element-scroll) [of](http://github.hubspot.com/tether/examples/enable-disable) [interesting]() [problems](http://github.hubspot.com/tether/examples/viewport); it ensures UI elements stay where they need to be, based on the various user interactions (click, scroll, etc) and layout contexts (fixed positioning, inside scrollable containers, etc).
11
12Please have a look at the [documentation](http://github.hubspot.com/tether/) for a more detailed explanation of why you might need Tether for your next project.
13
14## What to Use Tether for and When to Use It
15
16Tether is a small, focused JavaScript library. For those who might be new to JavaScript, a library is simply a JavaScript file (or files) that contain useful JavaScript code to help achieve tasks easier and faster. Since Tether is a JavaScript user interface (**UI**) library, it contains code to help you to manage the way your website or web app appears.
17
18Tether’s goal to is to help you position your elements side-by-side when needed.
19
20Let’s say you’ve started working on your dream project—a fancy web app that’s sure to become the next big thing! An important feature of your new app is to allow users to comment on shared photos. However, due to limited vertical space and the overall layout of your new app, you’d like to display the comments **next** to the image, similar to how Instagram does it.
21
22Your HTML code might look something like this:
23
24```html
25<div class="container">
26 <img src="awesome-picture.jpg" alt="Awesome Picture" class="picture">
27 <div class="comments">
28 ...
29 </div>
30</div>
31```
32
33Now, you could achieve this with some CSS using its `position` property, but going this route can be problematic since many of `position`’s values take elements **out** of the natural DOM flow. For example, if you have an element at the bottom of your HTML document, using `position: absolute` or `position: fixed` might could move it all the way to the top of your website in the browser.
34
35Not only that, but you also have to make manual adjustments to ensure **other** elements aren’t negatively affected by the positioned elements. Not to mention, you probably want your comment box to be **responsive**, and look good across different device sizes. Coding a solution for this manually is a challenge all on its own.
36
37**Enter Tether!**
38
39After installing Tether and including it in your project, you can begin using it!
40
411. In your JavaScript file, create a new instance (or constructor function) of the `Tether` object:
42
43 ```javascript
44 new Tether({});
45 ```
46
472. Within the curly braces (`{}`) you can configure the library’s options. Tether’s extensive list of options can be found in the [Tether documentation](http://github.hubspot.com/tether/).
48
49 ```javascript
50 new Tether({
51 element: '.comments',
52 target: '.picture',
53 attachment: 'top right'
54 targetAttachment: 'top left'
55 });
56 ```
57
58Now you have a perfectly placed comment section to go with your awesome picture! It’ll even stay attached to the element when a user resizes their browser window.
59
60There are tons of other useful features of Tether as well, instead of “comment boxes” you could also build:
61
62* Tooltips for useful hints and tricks,
63* Dropdown menus,
64* Autocomplete popups for forms,
65* and [more](http://github.hubspot.com/tether/examples/list_of_examples/)!
66
67## Install
68
69__npm__
70```sh
71$ npm install tether
72```
73
74__bower__
75```sh
76$ bower install tether
77```
78
79__download__
80
81Or just download from the [releases](https://github.com/HubSpot/tether/releases).
82
83## Usage
84You only need to include [tether.min.js](https://github.com/HubSpot/tether/blob/master/dist/js/tether.min.js) in your page:
85```
86<script src="path/to/dist/js/tether.min.js"></script>
87```
88Or just use a CDN:
89```
90<script src="//cdnjs.cloudflare.com/ajax/libs/tether/1.3.1/js/tether.min.js"></script>
91```
92
93The css files in the [dist/css](https://github.com/HubSpot/tether/tree/master/dist/css) folder are not required to get tether running.
94
95For more details jump straight in to the detailed [Usage](http://github.hubspot.com/tether/#usage) page.
96
97[![Tether Docs](http://i.imgur.com/YCx8cLr.png)](http://github.hubspot.com/tether/#usage)
98
99[Demo & API Documentation](http://github.hubspot.com/tether/)
100
101## Contributing
102
103We encourage contributions of all kinds. If you would like to contribute in some way, please review our [guidelines for contributing](CONTRIBUTING.md).
104
105## License
106Copyright &copy; 2014-2016 HubSpot - [MIT License](LICENSE)