jQuery Before After
A jQuery plugin for adding :before and :after support to browsers without generated content features.
Getting Started
Download the production version or the development version.
For parsing a single stylesheet
<link rel="stylesheet" type="text/css" href="styles.css" />
<script src="jquery.js"></script>
<script src="dist/jquery.beforeAfter.min.js"></script>
<script>
jQuery(function($) {
$(document)
.beforeAfter({
stylesheet: 'styles.css'
});
});
</script>
For parsing all linked stylesheets
<link rel="stylesheet" type="text/css" href="styles-1.css" />
<link rel="stylesheet" type="text/css" href="styles-2.css" />
<script src="jquery.js"></script>
<script src="dist/jquery.beforeAfter.min.js"></script>
<script>
jQuery(function($) {
$(document).beforeAfter();
});
</script>
Documentation
Add the script after your stylesheet and jQuery, then create your css with before and after pseudo class as well as the respective class preceded by a direct child selector >
.
#wrapper h1:before, #wrapper h1 > .before
Currently the plugin only supports single characters values being passed through the content property
For example
#wrapper h1:before {
content: '.'
}
#wrapper h1:after {
content: '...'
}
Would yield
<div id=#wrapper">
<div class="before">.</div>
<div class="after">.</div>
</div>
I'm aware of the problem and will be fixing it in future releases
Supported
- Non-ordinal pseudo selectors
:before and :after
- Multiple and single stylesheets
- Single stylesheet configuration option
{stylesheet:'styes.css'}
- Single character
content: "."
content injection
Unsupported
- Multiple ::before and ::after pseudo-elements using ordinals
:before(#), :after(#)
- Chained pseudo selectors
:first-child:after
- Multiple character
content: "..."
content injection
Examples
Stylesheet
#wrapper h1:before, #wrapper h1 > .before {
content: ">";
margin-right: 10px;
color: white; }
#wrapper h1:after, #wrapper h1 > .before {
content: "_";
color: white; }
#wrapper p:before, #wrapper p > .before {
font-family: impact;
content: "“";
font-size: 52px;
color: grey;
line-height: auto; }
#wrapper p:after, #wrapper p > .after {
content: "”";
font-family: impact;
font-size: 52px;
color: grey; }
Note the class selectors on the same line as the pseudo class
#wrapper h1:before, #wrapper h1 > .before
While the plugin will not add true pseudo classes you will get containers .before, .after
that will behave exactly the same.
Document
<!-- Its recommended that you use Modernizer with generated content in the build -->
<script src="modernizr.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css" />
<div id="wrapper">
<h1>Lorem ipsum dolor sit amet, consectetur adipisicing elit</h1>
<p>Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<script src="jquery.js"></script>
<script src="jquery.beforeAfter.js"></script>
<script>
// It is not essential to pass a stylesheet
// but doing so may improve performance and
// avoid the plugin processing css that was
// not built with it in mind.
$(document)
.beforeAfter({
stylesheet: 'styles.css'
});
</script>
Release History
(Nothing yet)
License
Copyright (c) 2012
Licensed under the MIT, GPL licenses.
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.
Important notes
Please don't edit files in the dist
subdirectory as they are generated via grunt. You'll find source code in the src
subdirectory!
While grunt can run the included unit tests via PhantomJS, this shouldn't be considered a substitute for the real thing. Please be sure to test the test/*.html
unit test file(s) in actual browsers.
Installing grunt
This assumes you have node.js and npm installed already.
- Test that grunt is installed globally by running
grunt --version
at the command-line. - If grunt isn't installed globally, run
npm install -g grunt
to install the latest version. You may need to runsudo npm install -g grunt
. - From the root directory of this project, run
npm install
to install the project's dependencies.
Installing PhantomJS
In order for the qunit task to work properly, PhantomJS must be installed and in the system PATH (if you can run "phantomjs" at the command line, this task should work).
Unfortunately, PhantomJS cannot be installed automatically via npm or grunt, so you need to install it yourself. There are a number of ways to install PhantomJS.
- PhantomJS and Mac OS X
- PhantomJS Installation (PhantomJS wiki)
Note that the phantomjs
executable needs to be in the system PATH
for grunt to see it.