Using native text selection causes displaying CAB bar (Android) or actions buttons (iOS) which are often undesired in HTML5-based mobile applications. We created jQuery Custom Selection to avoid native text selection appearance, especially in following environments of HTML5 applications:
- Flex (StageWebView)
- Apache Cordova / PhoneGap
- mobile website
-
Include dependencies:
-
jQuery
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
-
<script src="bower_components/hammerjs/hammer.min.js"></script>
-
-
Include plugin's code:
<script src="dist/jquery.custom-selection.min.js"></script>
-
Call the plugin:
$('body').customSelection();
Example advanced initialization via jQuery plugin:
var iframeBody = $('iframe')[0].contentDocument.body;
var options = {
startMarker: $('.selection-marker-start'),
endMarker: $('.selection-marker-end'),
onSelectionChange: function(range) {},
contentOrigin: {
scale: 0.7,
offsetX: 100,
offsetY: 35
}
};
$(iframeBody).customSelection(options);
The same effect we can accomplish using classic object-oriented API:
var customSelection = new CustomSelection(iframeBody, options);
Refreshes the selection drawing and the markers position. Useful after a screen resize or change of the iframe scale.
var contentOrigin = {
scale: 0.8,
offsetX: 80,
offsetY: 25
};
$(iframeBody).refreshCustomSelection(contentOrigin);
or
customSelection.refresh(contentOrigin);
Clears current selection and calls onSelectionChange
callback with an empty range.
$(iframeBody).clearCustomSelection();
or
customSelection.clear();
Disables Custom Selection and restores a native text selection. It is possible to re-enable Custom Selection using a next initialization.
$(iframeBody).disableCustomSelection();
or
customSelection.destroy();
Great thanks for Young Digital Planet for supporting creation of this plugin.